我正在嘗試打印切片。文檔說要與第 0 個元素的地址一起使用。這只給出了切片的地址。 用十六進(jìn)制打印切片,我找不到阻止它的方法。fmt%p%q在 go 中打印切片時要使用的正確動詞是什么?(我目前只使用%v,它的工作原理)func main() { slice := []int{0,1,2,3} fmt.Printf("slice: %p\n",slice)}//output is "slice: 0xc00007a000" or other memory address//using &slice[0] is no different. Using slice[0] gives a type error
1 回答

慕的地10843
TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個贊
%v通常,這是一個不錯的選擇,但您也可以對切片元素使用說明符:
package main
import "fmt"
func main() {
{
s := []int{10, 20, 30, 40}
fmt.Printf("%d\n", s) // [10 20 30 40]
}
{
s := []byte{'e', 'a', 's', 't'}
fmt.Printf("%c\n", s) // [e a s t]
fmt.Printf("%s\n", s) // east
fmt.Printf("%q\n", s) // "east"
}
{
s := []string{"east", "west"}
fmt.Printf("%s\n", s) // [east west]
fmt.Printf("%q\n", s) // ["east" "west"]
}
}
- 1 回答
- 0 關(guān)注
- 105 瀏覽
添加回答
舉報
0/150
提交
取消