$ goeval 'fmt.Println("Hello, world!")'
Hello, world!
$ goeval 'fmt.Println(os.Args[1])' 'Hello, world!'
Hello, world!
$ goeval -i .=fmt -i os 'Println(os.Args[1])' 'Hello, world!'
Hello, world!
$ goeval -i math/rand 'fmt.Println(rand.Int())'
5577006791947779410
$ goeval -i fmt -i math/big -i os 'var x, y, z big.Int; x.SetString(os.Args[1], 10); y.SetString(os.Args[2], 10); fmt.Println(z.Mul(&x, &y).String())' 45673432245678899065433367889424354 136762347343433356789893322
6246405805150306996814033892780381988744339134177555648763988
$ # Use os.Args
$ goeval 'fmt.Printf("%x\n", sha256.Sum256([]byte(os.Args[1])))' abc
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
$ # Download module in GOPATH mode and use with goeval
$ GO111MODULE=off go get github.com/klauspost/cpuid && goeval -i github.com/klauspost/cpuid/v2 'fmt.Println(cpuid.CPU.X64Level())'
3
$ # Serve the current directory over HTTP
$ goeval 'http.Handle("/",http.FileServer(http.Dir(".")));http.ListenAndServe(":8084",nil)'
$ # Import net/http symbols in package scope for shorter code
$ goeval -i .=net/http 'Handle("/",FileServer(Dir(".")));ListenAndServe(":8084",nil)'
Use -i <module>@<version> to import a Go module.
Use -i <alias>=<module>@<version> to import a Go module and import the package with the given alias.
$
goeval -i .=github.com/bitfield/[email protected] 'ListFiles("./[^.]*").First(4).Stdout()'
LICENSE
README.md
doc.go
go.mod
$
goeval -i github.com/klauspost/cpuid/[email protected] -i github.com/klauspost/cpuid/v2 'fmt.Println(cpuid.CPU.X64Level())'
3
$
goeval -i cpuid=github.com/klauspost/cpuid/[email protected] 'fmt.Println(cpuid.CPU.X64Level())'
3
Run your code on the Go Playground, and show output on the terminal:
$ goeval -play 'fmt.Println(time.Now())'
2009-11-10 23:00:00 +0000 UTC m=+0.000000001
Show the code sent to the Go Playground:
$ goeval -Eplay 'fmt.Println(time.Now())'
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println(time.Now())
}
Share the code on go.dev:
$ goeval -share 'fmt.Println(time.Now())'
https://go.dev/play/p/Z35Vf8gIg4Z
$ go install github.com/dolmen-go/goeval@latest
$ go clean -i github.com/dolmen-go/goeval
goeval just wraps your code with the necessary text to build a main package and a main func with the given imports, pass it through the goimports tool (to automatically add missing imports), writes in a temporary file and calls go run with GO111MODULE=off.
goimports is enabled by default, but you can disable it to force explicit imports (for forward safety):
$ goeval -goimports= -i fmt 'fmt.Println("Hello, world!")'
Hello, world!
When at least one module@version is imported with -i, Go module mode is enabled. Two files are generated: tmpxxxx.go and go.mod. Then go get . is run to resolve and fetch dependencies, and then go run.
To debug a syntax error:
$ goeval -E -goimports= ... | goimports
Here are some tricks that have worked in the past, that may still work in the last version, but are not guaranteed to work later.
The supported way:
$ goeval 'var fact func(int)int;fact=func(n int)int{if n==1{return 1};return n*fact(n-1)};fmt.Println(fact(5))'
The hacky way:
$ goeval 'fmt.Println(fact(5))};func fact(n int)int{if n==1{return 1};return n*fact(n-1)'
Needs:
- goeval compiled with Go 1.18+
- Go 1.18+ installed.
$ goeval 'p(1);p("a");};func p[T any](x T){fmt.Println(x)'
1
a
$ goeval 'p(1);p(2.0);};func p[T int|float64](x T){x++;fmt.Println(x)'
2
3
$ goeval -i golang.org/x/exp/constraints 'p(1);p(2.0);};func p[T constraints.Signed|constraints.Float](x T){x++;fmt.Println(x)'
2
3
- gommand Go one liner program. Similar to python -c.
- gorram Like go run for any Go function.
- goexec A command line tool to execute Go functions.
Copyright 2019-2025 Olivier Mengué
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.