forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example4.go
49 lines (38 loc) · 1.25 KB
/
example4.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// https://docs.google.com/document/d/1CxgUBPlx9iJzkz9JWkb6tIpTe5q32QDmz8l0BouG0Cw/view
// http://play.golang.org/p/_uK8EYlsd0
// go build -gcflags -m
// Sample program to show varaibles stay on or escape from the stack.
package main
import "fmt"
// main is the entry point for the application.
func main() {
stayOnStack()
escapeToHeap()
}
// stayOnStack shows how the variable does not escape.
func stayOnStack() {
// Declare a variable of type integer.
var x int
// Display the address of the variable.
println("Stack Addr:", &x)
}
// escapeToHeap shows how the variable does escape.
func escapeToHeap() {
// Declare a variable of type integer.
var x int
// Display the address of the variable.
fmt.Println("Heap Addr:", &x)
}
/*
./example1.go:22: can inline stayOnStack
./example1.go:17: inlining call to stayOnStack
./example1.go:36: "Heap Addr:" escapes to heap
./example1.go:36: &x escapes to heap
./example1.go:33: moved to heap: x
./example1.go:36: &x escapes to heap
./example1.go:36: escapeToHeap ... argument does not escape
./example1.go:17: main &x does not escape
./example1.go:27: stayOnStack &x does not escape
*/