Go 1.24 has added a new -json
flag to go build
, go install
& go test
.
This makes it easier to parse output.
I’ve added a few examples to this post.
go build #
package main
import (
"fmt"
)
func main() {
fmt.Prrintln("Hello World")
}
go build
will output:
# github.com/alrayyes/golanghelloworld
./main.go:8:6: undefined: fmt.Prrintln
go build -json
will output:
{"ImportPath":"github.com/alrayyes/golanghelloworld","Action":"build-output","Output":"# github.com/alrayyes/golanghelloworld\n"}
{"ImportPath":"github.com/alrayyes/golanghelloworld","Action":"build-output","Output":"./main.go:8:6: undefined: fmt.Prrintln\n"}
{"ImportPath":"github.com/alrayyes/golanghelloworld","Action":"build-fail"}
go install #
package main
import (
"fmt"
)
func main() {
fmt.Prrintln("Hello World")
}
go install
will output:
# github.com/alrayyes/golanghelloworld
./main.go:8:6: undefined: fmt.Printlln
go install -json
will output:
{"ImportPath":"github.com/alrayyes/golanghelloworld","Action":"build-output","Output":"# github.com/alrayyes/golanghelloworld\n"}
{"ImportPath":"github.com/alrayyes/golanghelloworld","Action":"build-output","Output":"./main.go:8:6: undefined: fmt.Printlln\n"}
{"ImportPath":"github.com/alrayyes/golanghelloworld","Action":"build-fail"}
go test #
package main
import (
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"io"
"os"
"testing"
)
func main() {
fmt.Println("Hello World")
}
func TestCmdOutput(t *testing.T) {
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
main()
_ = w.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
got := buf.String()
want := "Hello World\n"
assert.Equal(t, want, got)
}
go test
will output:
PASS
ok github.com/alrayyes/golanghelloworld 0.002s
go build -json
will output:
{"Time":"2025-02-14T17:39:12.407851295+01:00","Action":"start","Package":"github.com/alrayyes/golanghelloworld"}
{"Time":"2025-02-14T17:39:12.409705024+01:00","Action":"run","Package":"github.com/alrayyes/golanghelloworld","Test":"TestCmdOutput"}
{"Time":"2025-02-14T17:39:12.409719621+01:00","Action":"output","Package":"github.com/alrayyes/golanghelloworld","Test":"TestCmdOutput","Output":"=== RUN TestCmdOutput\n"}
{"Time":"2025-02-14T17:39:12.409736633+01:00","Action":"output","Package":"github.com/alrayyes/golanghelloworld","Test":"TestCmdOutput","Output":"--- PASS: TestCmdOutput (0.00s)\n"}
{"Time":"2025-02-14T17:39:12.409741492+01:00","Action":"pass","Package":"github.com/alrayyes/golanghelloworld","Test":"TestCmdOutput","Elapsed":0}
{"Time":"2025-02-14T17:39:12.409747975+01:00","Action":"output","Package":"github.com/alrayyes/golanghelloworld","Output":"PASS\n"}
{"Time":"2025-02-14T17:39:12.410061284+01:00","Action":"output","Package":"github.com/alrayyes/golanghelloworld","Output":"ok \tgithub.com/alrayyes/golanghelloworld\t0.002s\n"}
{"Time":"2025-02-14T17:39:12.410074068+01:00","Action":"pass","Package":"github.com/alrayyes/golanghelloworld","Elapsed":0.002}
Example code #
The example code can be found here:
alrayyes/golanghelloworld
Golang hello world
Shell
0
0