- go 1.13
- goimports
- gofmt
go get golang.org/x/tools/cmd/goimports
go get github.com/golobby/repl
go install github.com/golobby/repl
repl is a REPL, Read-Eval-Print-Loop. In scripting languages like PHP or Python there is an environment called repl, which is basically a command line interface that is connected to the language interpreter and it would show instantly the result of user input. In Golang we don't have this feature by default but it does'nt mean that we cannot benefit the power that repl gives us.
repl parses the input and put the code you entered in one of these categories:
- Imports
- Var declaration/assignment
- type definition
- function definition
- function calls
then repl creates a file from state of the session running.
repl Prompt -> goimports -> go compiler -> shows result of compile
In a repl you need to have access to the vars defined no matter what scope you are in, repl provides this feature by defining all vars in a global scope.
repl> func someFunc() string {
...repl> return fmt.Sprint(a)
...repl> }
repl> a = 2
repl> someFunc() // 2
repl does not care about either type or value of a variable, so you can redefine or change type of variable with ease.
repl> x := 3
repl> x := "amirreza"
repl> x := someType{}
repl> var x = 5
// all above codes are valid in a repl interpreter
repl can easily evaluate any valid Go expression.
repl> 2 // <int> 2
repl> 2*3*(2+1) // <int> 18
repl> "Hello World" // <string> "Hello World"
repl uses the power of goimports so it can almost identify all packages you use and automatically import them for you.
repl> fmt.Println("Hello") // no need for manual import, goimports will take care of that
repl does not suppress any error message so you can see exact error message from go toolchain
repl build from ground up to be compatible with go modules, so when you fire up a repl in go project which is a go module project repl instantly identifies your module and imports it as a module with local path, so you can access all your public types and functions.(watch Demo)
go doc is available as a shell command so you can access any document about any package with repl.
repl> :doc fmt
repl> :doc json.Marshal
repl> :e 1+2
// <int> 3
repl> :e "HelloWorld"
// <string> "HelloWorld"
shows formatted view of current session state.
repl> :dump
pops latest entered code from session.
repl> :pop
file shows exactly code that will be generated from current state of session.
repl> :file