Share
Embed
Transcript
28 slides
-
func main() { a := [4]int{1, 2, 3, 4} println("a
addr:", &a) for i := range a { print("Value: ", a[i]) println(" Address:", &a[i]) } } -
on 64bit architecture, each int = 8 bytes func main()
{ a := [4]int{1, 2, 3, 4} println("a addr:", &a) for i := range a { print("Value: ", a[i]) println(" Address:", &a[i]) } } a addr: 0x220822bf20 Value: 1 IndexAddr: 0x220822bf20 Value: 2 IndexAddr: 0x220822bf28 Value: 3 IndexAddr: 0x220822bf30 Value: 4 IndexAddr: 0x220822bf38 -
In Go, everything is pass by value. When we pass
an array as an argument, we pass a copy of the array not a reference to the array. package main func main() { names := [4]string {"ada", "lovelace", "tom", "jerry"} f1(names) println(names[0]) } func f1(a [4]string) { a[0] = "marie" } -
1. A copy of names is made when f1 is
called 1 package main func main() { names := [4]string {"ada", "lovelace", "tom", "jerry"} f1(names) println(names[0]) } func f1(a [4]string) { a[0] = "marie" } -
2. The copy of names is assigned to local var
a 1 package main func main() { names := [4]string {"ada", "lovelace", "tom", "jerry"} f1(names) println(names[0]) } func f1(a [4]string) { a[0] = "marie" } 2 -
3. We make a change to the copy 3 2
1 package main func main() { names := [4]string {"ada", "lovelace", "tom", "jerry"} f1(names) println(names[0]) } func f1(a [4]string) { a[0] = "marie" } -
3 2 1 package main func main() { names :=
[4]string {"ada", "lovelace", "tom", "jerry"} f1(names) println(names[0]) } func f1(a [4]string) { a[0] = "marie" } // ada -
If we want to share names with f1 so f1
can modify it, then we pass the address of names to f1. package main func main() { names := [4]string {"ada", "lovelace", "tom", "jerry"} f1(&names) println(names[0]) } func f1(a *[4]string) { a[0] = "marie" } -
1. Pass the address of names package main func main()
{ names := [4]string {"ada", "lovelace", "tom", "jerry"} f1(&names) println(names[0]) } func f1(a *[4]string) { a[0] = "marie" } 1 -
2. f1 accepts a pointer variable package main func main()
{ names := [4]string {"ada", "lovelace", "tom", "jerry"} f1(&names) println(names[0]) } func f1(a *[4]string) { a[0] = "marie" } 2 1 -
3. Changes to a update the array that a points
to, names. package main func main() { names := [4]string {"ada", "lovelace", "tom", "jerry"} f1(&names) println(names[0]) } func f1(a *[4]string) { a[0] = "marie" } 3 2 1 -
package main func main() { names := [4]string {"ada", "lovelace",
"tom", "jerry"} f1(&names) println(names[0]) } func f1(a *[4]string) { a[0] = "marie" } 3 2 1 // marie -
func main() { a := [4]int{1, 2, 3, 4} println("a
addr:", &a) for i := range a { print("Value: ", a[i]) println(" Address:", &a[i]) } } print -
func main() { a := [4]int{1, 2, 3, 4} println("a
addr:", &a) for i := range a { print("Value: ", a[i]) println(" Address:", &a[i]) } } print func main() { a := [4]int{1, 2, 3, 4} println("a addr:", &a) for i := range a { print("Value: ", a[i]) println(" Address:", &a[i]) } } import "fmt" func main() { a := [4]int{1, 2, 3, 4} fmt.Printf("a addr: %p \n", &a) for i := range a { fmt.Printf("V: %v A: %p \n", a[i], &a[i]) } } import "fmt" func main() { a := [4]int{1, 2, 3, 4} fmt.Printf("a addr: %p \n", &a) for i := range a { fmt.Printf("V: %v A: %p \n", a[i], &a[i]) } } fmt.Print -
1. escape analysis go build -gcflags=-m main.go:6: moved to heap:
a main.go:8: &a escapes to heap main.go:11: &a[i] escapes to heap main.go:8: main ... argument does not escape main.go:11: main ... argument does not escape with ft.Printf -
1. inlining func main() { names := [2]string{"ada", "lovelace"} f1(names)
println(names[0]) } func f1(a [2]string) { a[0] = "marie" } -
inlining func main() { names := [2]string{"ada", "lovelace"} f1(names) println(names[0])
} func f1(a [2]string) { a[0] = "marie" // Do this to prevent inlining. var x int fmt.Sprintf("Prevent Inlining: %d", x) }