{"id":22540604,"url":"https://github.com/dolmen-go/goeval","last_synced_at":"2025-04-09T21:45:16.095Z","repository":{"id":57486895,"uuid":"190236118","full_name":"dolmen-go/goeval","owner":"dolmen-go","description":"Run Go snippets instantly from the command-line","archived":false,"fork":false,"pushed_at":"2025-04-02T16:20:36.000Z","size":46,"stargazers_count":24,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T17:29:31.413Z","etag":null,"topics":["go","golang","golang-tools"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dolmen-go.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-04T16:01:43.000Z","updated_at":"2025-04-02T16:20:39.000Z","dependencies_parsed_at":"2023-02-18T17:10:17.529Z","dependency_job_id":"9ff9a114-a156-4d7e-bb9c-352235db74ec","html_url":"https://github.com/dolmen-go/goeval","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dolmen-go%2Fgoeval","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dolmen-go%2Fgoeval/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dolmen-go%2Fgoeval/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dolmen-go%2Fgoeval/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dolmen-go","download_url":"https://codeload.github.com/dolmen-go/goeval/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248119439,"owners_count":21050754,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","golang-tools"],"created_at":"2024-12-07T12:11:39.004Z","updated_at":"2025-04-09T21:45:16.086Z","avatar_url":"https://github.com/dolmen-go.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# goeval - Evaluate Go snippets instantly from the command line\n\n## Demo\n\n```console\n$ goeval 'fmt.Println(\"Hello, world!\")'\nHello, world!\n$ goeval 'fmt.Println(os.Args[1])' 'Hello, world!'\nHello, world!\n$ goeval -i .=fmt -i os 'Println(os.Args[1])' 'Hello, world!'\nHello, world!\n$ goeval -i math/rand 'fmt.Println(rand.Int())'\n5577006791947779410\n\n$ 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(\u0026x, \u0026y).String())' 45673432245678899065433367889424354 136762347343433356789893322\n6246405805150306996814033892780381988744339134177555648763988\n\n$ goeval 'fmt.Printf(\"%x\\n\", sha256.Sum256([]byte(os.Args[1])))' abc\nba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad\n\n$ GO111MODULE=off go get github.com/klauspost/cpuid \u0026\u0026 goeval -i github.com/klauspost/cpuid/v2 'fmt.Println(cpuid.CPU.X64Level())'\n3\n\n$ goeval 'http.Handle(\"/\",http.FileServer(http.Dir(\".\")));http.ListenAndServe(\":8084\",nil)'\n\n$ goeval -i .=net/http 'Handle(\"/\",FileServer(Dir(\".\")));ListenAndServe(\":8084\",nil)'\n```\n\n### Go modules\n\nUse `-i \u003cmodule\u003e@\u003cversion\u003e` to import a Go module.\n\nUse `-i \u003calias\u003e=\u003cmodule\u003e@\u003cversion\u003e` to import a Go module and import the package with the given alias.\n\n```console\n$ goeval -i .=github.com/bitfield/script@v0.21.4 'Exec(\"ls\").Stdout()'\nLICENSE\nREADME.md\ngo.mod\ngo.sum\ngoeval\nmain.go\n\n$ goeval -i github.com/klauspost/cpuid/v2@v2.2.3 -i github.com/klauspost/cpuid/v2 'fmt.Println(cpuid.CPU.X64Level())'\n3\n$ goeval -i cpuid=github.com/klauspost/cpuid/v2@v2.2.3 'fmt.Println(cpuid.CPU.X64Level())'\n3\n```\n\n## Install\n\n```console\n$ go install github.com/dolmen-go/goeval@latest\n```\n\n## Uninstall\n\n```console\n$ go clean -i github.com/dolmen-go/goeval\n```\n\n## How does it work?\n\n### GOPATH mode\n\n`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](https://godoc.org/golang.org/x/tools/cmd/goimports) (to automatically add missing imports), writes in a temporary file and calls `go run` with [`GO111MODULE=off`](https://golang.org/ref/mod#mod-commands).\n\n`goimports` is enabled by default, but you can disable it to force explicit imports (for forward safety):\n\n```console\n$ goeval -goimports= -i fmt 'fmt.Println(\"Hello, world!\")'\nHello, world!\n```\n\n### Go module mode\n\nWhen 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`.\n\n## Debugging\n\nTo debug a syntax error:\n\n```console\n$ goeval -E -goimports= ... | goimports\n````\n\n## Unsupported tricks\n\nHere are some tricks that have worked in the past, that may still work in the last version, but are not guaranteed to work later.\n\n### Use functions\n\nThe supported way:\n\n```console\n$ 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))'\n```\n\nThe hacky way:\n\n```console\n$ goeval 'fmt.Println(fact(5))};func fact(n int)int{if n==1{return 1};return n*fact(n-1)'\n```\n\n### Use generics\n\nNeeds:\n- goeval compiled with Go 1.18+\n- Go 1.18+ installed.\n\n```console\n$ goeval 'p(1);p(\"a\");};func p[T any](x T){fmt.Println(x)'\n1\na\n$ goeval 'p(1);p(2.0);};func p[T int|float64](x T){x++;fmt.Println(x)'\n2\n3\n$ 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)'\n2\n3\n```\n\n## Alternatives\n\n* [gommand](https://github.com/sno6/gommand) Go one liner program. Similar to `python -c`.\n* [gorram](https://github.com/natefinch/gorram) Like `go run` for any Go function.\n* [goexec](https://github.com/shurcooL/goexec) A command line tool to execute Go functions.\n\n## License\n\nCopyright 2019-2025 Olivier Mengué\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n   http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdolmen-go%2Fgoeval","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdolmen-go%2Fgoeval","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdolmen-go%2Fgoeval/lists"}