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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| package main
import ( "fmt" "reflect" "unsafe" )
func printInt(i int) { fmt.Printf("int i: %p\n", &i) }
func printInt2(i *int) { fmt.Printf("int i: %p\n", i) }
func printStr(s string) { fmt.Printf("string s: %p\n", &s)
hdr := (*reflect.StringHeader)(unsafe.Pointer(&s)) data := hdr.Data fmt.Printf("string s data: 0x%x\n", data)
}
func printStr2(s *string) { fmt.Printf("string s: %p\n", s) hdr := (*reflect.StringHeader)(unsafe.Pointer(s)) data := hdr.Data fmt.Printf("string s data: 0x%x\n", data) }
func printSlice(s []int) { fmt.Printf("slice s: %p\n", &s)
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&s)) data := hdr.Data fmt.Printf("slice s data: 0x%x\n", data)
}
func printSlice2(s *[]int) { fmt.Printf("slice s: %p\n", s) hdr := (*reflect.SliceHeader)(unsafe.Pointer(s)) data := hdr.Data fmt.Printf("slice s data: 0x%x\n", data) }
type S struct { I int }
func printStruct(s S) { fmt.Printf("struct s: %p, I: %p\n", &s, &(s.I)) }
func printStruct2(s *S) { fmt.Printf("struct s: %p, I: %p\n", &s, &(s.I)) }
func printInterface(i interface{}) { fmt.Printf("int i: %p\n", &i) }
func printInterface2(i interface{}) { s := i.(S) fmt.Printf("struct s: %p, I: %p\n", &s, &(s.I)) }
func printInterface3(i interface{}) { s := i.(*S) fmt.Printf("struct s: %p, I: %p\n", s, &(s.I)) }
func main() { i := 10 fmt.Printf("int i: %p\n", &i) printInt(i) printInt2(&i)
s := "hello, world" fmt.Printf("\n\nstring s: %p\n", &s) hdr := (*reflect.StringHeader)(unsafe.Pointer(&s)) data := hdr.Data fmt.Printf("string s data: 0x%x\n", data) printStr(s) printStr2(&s)
sl := []int{1, 2, 3, 4, 5} fmt.Printf("\n\nslice s: %p\n", &sl) hdr2 := (*reflect.SliceHeader)(unsafe.Pointer(&sl)) data = hdr2.Data fmt.Printf("slice s data: 0x%x\n", data) printSlice(sl) printSlice2(&sl)
ss := S{I: 10} ssp := &ss fmt.Printf("\n\nstruct s: %p, I: %p\n", ssp, &(ss.I)) printStruct(ss) printStruct2(ssp)
fmt.Printf("\n\nint i: %p\n", &i) printInterface(i) fmt.Printf("\n\nstruct s: %p, I: %p\n", ssp, &(ss.I)) printInterface2(ss) printInterface3(ssp) }
|