Skip to content

Instantly share code, notes, and snippets.

@jbowens
Created September 8, 2017 20:38
Show Gist options
  • Save jbowens/30f0854e7e5cec46cce5e2d78ec2c943 to your computer and use it in GitHub Desktop.
Save jbowens/30f0854e7e5cec46cce5e2d78ec2c943 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
)
func main() {
functionWithBuffer()
}
func functionWithBuffer() {
var buf bytes.Buffer
for i := 0; i < 100; i++ {
buf.WriteByte(byte(i))
}
fmt.Println(buf.Len())
}
package main
import (
"bytes"
"fmt"
)
func main() {
functionWithBuffer()
}
func functionWithBuffer() {
buf := new(bytes.Buffer)
for i := 0; i < 100; i++ {
buf.WriteByte(byte(i))
}
fmt.Println(buf.Len())
}
jackson@mba ~/src/scratch/goescape $ go build -gcflags '-m' a.go
# command-line-arguments
./a.go:17:21: inlining call to bytes.(*Buffer).Len
./a.go:15:6: buf escapes to heap
./a.go:13:6: moved to heap: buf
./a.go:17:21: buf.Len() escapes to heap
./a.go:17:21: functionWithBuffer buf does not escape
./a.go:17:13: functionWithBuffer ... argument does not escape
jackson@mba ~/src/scratch/goescape $ go build -gcflags '-m' b.go
# command-line-arguments
./b.go:17:21: inlining call to bytes.(*Buffer).Len
./b.go:13:12: new(bytes.Buffer) escapes to heap
./b.go:17:21: buf.Len() escapes to heap
./b.go:17:13: functionWithBuffer ... argument does not escape
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment