我在下面创建了 2 个 Vertex 对象 - q 和 q。
现在,当我打印指针变量 q = &Vertex 时,我希望它是内存地址,为什么它打印 - &{1,2}
输出:
{1 2} &{1 2}
程序:
package main
import "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)
}
最佳答案
来自 https://golang.org/pkg/fmt/ :
Println formats using the default formats for its operands
如果您想以特定方式打印,您需要使用 fmt.Printf()
并提供您想要的格式。
关于go - 打印指针变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51953778/