Consider the following benchmark:
package main
import (
"io/ioutil"
"os"
"os/exec"
"testing"
)
func BenchmarkRun(b *testing.B) {
for i := 0; i < b.N; i++ {
source := `package main
import "fmt"
func main() {
fmt.Println("foo")
}`
if err := ioutil.WriteFile("cmd.go", []byte(source), 0777); err != nil {
b.Error(err)
}
defer os.Remove("cmd.go")
if err := exec.Command("go", "run", "cmd.go").Run(); err != nil {
b.Error(err)
}
}
}
This takes around 0.3sec per operation.
Is there any way of speeding up a compile / run cycle?
It seems clumsy to write a temporary file and exec go run. Is there a way to invoke the compiler without doing this?