-cover
to generate coverage information¶%goflags -cover
%goflags=["-cover"]
%goflags
%goflags=["-cover"]
// Clear %goflags
%goflags ""
%goflags=[]
%goflags -cover
%goflags=["-cover"]
GOCOVERDIR
directory to hold coverage information¶%env GOCOVERDIR /tmp/gonb_examples_test_goflags_cover
!mkdir -p $GOCOVERDIR ; rm -f ${GOCOVERDIR}/*
Set: GOCOVERDIR="/tmp/gonb_examples_test_goflags_cover"
func A() {
fmt.Println("A")
}
func B() {
fmt.Println("B")
}
%%
A()
A
We expect that function A has full coverage, and function B has 0% coverage, since it was not called.
!go tool covdata func -i "${GOCOVERDIR}"
gonb_a636feef/main.go:8: A 100.0% gonb_a636feef/main.go:12: B 0.0% gonb_a636feef/main.go:17: main 100.0% total (statements) 75.0%
go build
to inspect its output¶This can be achieved by manually running it, independent of GoNB
, simply using it's ability of running shell commands in the temporary directory.
First we clear the cache of GoNB
, using %reset
and then we create a small program to analyse if variables escape to heap (Go's garbage collection flag -gcflag=-m
).
%reset
* State reset: all memorized declarations discarded.
type Point struct{ X, Y float64}
func (p *Point) ManhattanLen() float64 {
return p.X + p.Y
}
%%
p := Point{3,4}
fmt.Println(p.ManhattanLen())
7
Display optimization strategy of the code:
!*go build -gcflags=-m
# gonb_a636feef ./main.go:10:6: can inline (*Point).ManhattanLen ./main.go:16:12: inlining call to flag.Parse ./main.go:18:27: inlining call to (*Point).ManhattanLen ./main.go:18:12: inlining call to fmt.Println ./main.go:10:7: p does not escape ./main.go:18:12: ... argument does not escape ./main.go:18:27: ~r0 escapes to heap