gosnip

command module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2018 License: MIT Imports: 4 Imported by: 0

README

gosnip: run small snippets of Go code from the command line

GoDoc TravisCI Build AppVeyor Build

Package gosnip is a tool that allows you to run small snippets of Go code from the command line.

usage: gosnip [-d] [-i import ...] statements...

To download and install, use "go get":

$ go get github.com/benhoyt/gosnip

For simple uses, just specify one or more Go statements on the command line, and gosnip will roll them into a full Go program and run the result using "go run". Standard library imports are added automatically. Some examples:

$ gosnip 'fmt.Println("Hello world")'
Hello world

$ gosnip 'fmt.Println("Current time:")' 'fmt.Println(time.Now())'
Current time:
2018-11-24 16:18:47.101951 -0500 EST m=+0.000419239

The -i flag specifies an explicit import, which is needed for non-stdlib imports or to disambiguate between ambiguous stdlib imports such as "text/template" and "html/template" (multiple -i flags are allowed). For example, -i is needed for "rand" because the standard library has math/rand and crypto/rand):

$ gosnip -i math/rand 'fmt.Println(rand.Intn(100))'
81

The -d flag turns on debug mode, which prints the full program on stderr before running it. For example:

$ gosnip -d 'fmt.Println(time.Now())'
package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println(time.Now())
}
2018-11-24 16:33:56.681024 -0500 EST m=+0.000383308

The gosnip command-line tool is a thin wrapper around the "sniplib" package. To run Go snippets in your Go programs, see the sniplib docs.

Why?

I made gosnip because when coding in Go I often want to try little snippets of code to see what they do, for example, "how does format string %6.3f work again?" I could use the Go playground, but it's nice to be able to use a one-line command. Also, I often develop while offline on my bus commute, so don't have access to the online Go playground (yes, I know it's possible to run the Go playground locally).

License

gosnip is licensed under an open source MIT license.

Contact me

Have fun, and please contact me if you're using gosnip or have any feedback!

Documentation

Overview

Package gosnip is a tool that allows you to run small snippets of Go code from the command line.

usage: gosnip [-d] [-i import ...] statements...

For simple uses, just specify one or more Go statements on the command line, and gosnip will roll them into a full Go program and run the result using "go run". Standard library imports are added automatically. Some examples:

$ gosnip 'fmt.Println("Hello world")'
Hello world

$ gosnip 'fmt.Println("Current time:")' 'fmt.Println(time.Now())'
Current time:
2018-11-24 16:18:47.101951 -0500 EST m=+0.000419239

The -i flag specifies an explicit import, which is needed for non-stdlib imports or to disambiguate between ambiguous stdlib imports such as "text/template" and "html/template" (multiple -i flags are allowed). For example, -i is needed for "rand" because the standard library has math/rand and crypto/rand):

$ gosnip -i math/rand 'fmt.Println(rand.Intn(100))'
81

The -d flag turns on debug mode, which prints the full program on stderr before running it. For example:

$ gosnip -d 'fmt.Println(time.Now())'
package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println(time.Now())
}
2018-11-24 16:33:56.681024 -0500 EST m=+0.000383308

The gosnip command-line tool is a thin wrapper around the "sniplib" package. To run Go snippets in your Go programs, see the sniplib docs.

Directories

Path Synopsis
Package sniplib converts Go code snippets to full programs and runs them.
Package sniplib converts Go code snippets to full programs and runs them.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL