github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/pointer/testdata/arrays.go (about) 1 //go:build ignore 2 // +build ignore 3 4 package main 5 6 var unknown bool // defeat dead-code elimination 7 8 var a, b int 9 10 func array1() { 11 sliceA := make([]*int, 10) // @line a1make 12 sliceA[0] = &a 13 14 var sliceB []*int 15 sliceB = append(sliceB, &b) // @line a1append 16 17 print(sliceA) // @pointsto makeslice@a1make:16 18 print(sliceA[0]) // @pointsto command-line-arguments.a 19 20 print(sliceB) // @pointsto append@a1append:17 21 print(sliceB[100]) // @pointsto command-line-arguments.b 22 } 23 24 func array2() { 25 sliceA := make([]*int, 10) // @line a2make 26 sliceA[0] = &a 27 28 sliceB := sliceA[:] 29 30 print(sliceA) // @pointsto makeslice@a2make:16 31 print(sliceA[0]) // @pointsto command-line-arguments.a 32 33 print(sliceB) // @pointsto makeslice@a2make:16 34 print(sliceB[0]) // @pointsto command-line-arguments.a 35 } 36 37 func array3() { 38 a := []interface{}{"", 1} 39 b := []interface{}{true, func() {}} 40 print(a[0]) // @types string | int 41 print(b[0]) // @types bool | func() 42 } 43 44 // Test of append, copy, slice. 45 func array4() { 46 var s2 struct { // @line a4L0 47 a [3]int 48 b struct{ c, d int } 49 } 50 var sl1 = make([]*int, 10) // @line a4make 51 var someint int // @line a4L1 52 sl1[1] = &someint 53 sl2 := append(sl1, &s2.a[1]) // @line a4append1 54 print(sl1) // @pointsto makeslice@a4make:16 55 print(sl2) // @pointsto append@a4append1:15 | makeslice@a4make:16 56 print(sl1[0]) // @pointsto someint@a4L1:6 | s2.a[*]@a4L0:6 57 print(sl2[0]) // @pointsto someint@a4L1:6 | s2.a[*]@a4L0:6 58 59 // In z=append(x,y) we should observe flow from y[*] to x[*]. 60 var sl3 = make([]*int, 10) // @line a4L2 61 _ = append(sl3, &s2.a[1]) 62 print(sl3) // @pointsto makeslice@a4L2:16 63 print(sl3[0]) // @pointsto s2.a[*]@a4L0:6 64 65 var sl4 = []*int{&a} // @line a4L3 66 sl4a := append(sl4) // @line a4L4 67 print(sl4a) // @pointsto slicelit@a4L3:18 | append@a4L4:16 68 print(&sl4a[0]) // @pointsto slicelit[*]@a4L3:18 | append[*]@a4L4:16 69 print(sl4a[0]) // @pointsto command-line-arguments.a 70 71 var sl5 = []*int{&b} // @line a4L5 72 copy(sl5, sl4) 73 print(sl5) // @pointsto slicelit@a4L5:18 74 print(&sl5[0]) // @pointsto slicelit[*]@a4L5:18 75 print(sl5[0]) // @pointsto command-line-arguments.b | command-line-arguments.a 76 77 var sl6 = sl5[:0] 78 print(sl6) // @pointsto slicelit@a4L5:18 79 print(&sl6[0]) // @pointsto slicelit[*]@a4L5:18 80 print(sl6[0]) // @pointsto command-line-arguments.b | command-line-arguments.a 81 } 82 83 func array5() { 84 var arr [2]*int 85 arr[0] = &a 86 arr[1] = &b 87 88 var n int 89 print(arr[n]) // @pointsto command-line-arguments.a | command-line-arguments.b 90 } 91 92 func main() { 93 array1() 94 array2() 95 array3() 96 array4() 97 array5() 98 }