我在下面創建了 2 個 Vertex 對象——q 和 q。現在,當我打印指針變量 q = &Vertex 時,我希望它是內存地址,為什么它打印 - &{1,2}輸出:{1 2} &{1 2}程序:package mainimport "fmt"type Vertex struct { X, Y int}var ( p = Vertex{1, 2} // has type Vertex q = &Vertex{1, 2} // has type *Vertex)func main() { fmt.Println(p, q)}操場
2 回答

largeQ
TA貢獻2039條經驗 獲得超8個贊

Smart貓小萌
TA貢獻1911條經驗 獲得超7個贊
函數fmt.Println(...)
“ [使用]其操作數的默認格式”并根據fmt
包頭文檔:
%v the value in a default format
...
struct: {field0 field1 ...}
...
pointer to above: &{}, &[], &map[]
所以以下幾行實際上是相同的:
fmt.Println(p, q)
fmt.Printf("%v %v\n", p, q)
如果你想打印指針的內存地址,那么你應該使用%p格式動詞:
Pointer:
%p base 16 notation, with leading 0x
例如:
fmt.Printf("%p\n", q) // => 0x1953e4
- 2 回答
- 0 關注
- 168 瀏覽
添加回答
舉報
0/150
提交
取消