How to run Go (lang) code directly from terminal / command line?

I want to run simple go code directly from the terminal / command line. For instance:

go run "
package main
func main() {
println("hello")
}
"
hello

However, golang only allows code from a file to be executed. So maybe there are some ways to imitate him? Like this:

go run file.go < echo "...."

But there should be no files after the steps above.

+4
source share
2 answers

On the command line, only a project like go-repl will compile / run multiline source code without leaving the .gofile behind.
Alternative: gore :

$ gore
Enter one or more lines and hit ctrl-D
func test() string {return "hello"}
println(test())
^D
---------------------------------
hello

( Does Go REPL?")

go, , .

+4

Ubuntu gorun, . " ", /tmp.

https://wiki.ubuntu.com/gorun

, REPL, .

gorun Ubuntu, Linux, Vanilla Go

$ go get launchpad.net/gorun
+1

Source: https://habr.com/ru/post/1569808/


All Articles