Can you use go run for scripting? I wrote a small program to find out.
gorun.go
package main
import (
"fmt"
"os"
"os/exec"
"time"
)
func main() {
start := time.Now()
gogo := fmt.Sprintf(`package main
import "fmt"
func main() {
fmt.Printf("the time was %s\n")
}`, start)
os.WriteFile("gogo.go", []byte(gogo), 0666)
cmd := exec.Command("go", "run", "gogo.go")
cmd.Stdout = os.Stdout
cmd.Run()
end := time.Now()
fmt.Printf("that took %s\n", end.Sub(start))
}
$ go run gorun.go
the time was 2024-01-05 12:04:01.042488 -0500 EST m=+0.000077959
that took 329.81325ms
On my somewhat older m3 running openbsd, it takes about 330ms. On a less old chromebook, it takes about 100ms. Not bad. On an M1 macbook, it varies wildly between 150ms and 300ms.
So I think not fast enough for interactive use. Probably fast enough for a continuously changing cronjob, though I wonder why you wouldn’t change the inputs. But I guess if you’re a big believer in configuration through compilation, and also have constantly changing requirements, it’s fast enough to get by.
For comparison, yaegi is still 10x or more quicker at putting something on the screen.
go build
I almost exclusively run go build. Actually, I run make and that runs go build, because that’s how my fingers work. So it wasn’t until recently that I noticed that go run is as fast as it is. I was used to a very observable one second per build.
Turns out go build will run hg stat for every build, which go run skips, and that was the slowest part of the process. Easily remedied by switching to chg.
Posted 05 Jan 2024 17:23 by tedu Updated: 05 Jan 2024 17:23
Tagged:
go