go - 打印指针变量

标签 go

我在下面创建了 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)
}

Playground

最佳答案

来自 https://golang.org/pkg/fmt/ :

Println formats using the default formats for its operands

如果您想以特定方式打印,您需要使用 fmt.Printf() 并提供您想要的格式。

关于go - 打印指针变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51953778/

相关文章:

go - 如何打印golang程序中执行的所有方法

go - 如何使用 FFI 从 Dart 调用 Go lib

从 go 调用 C 函数

performance - 直接在源码中使用gobs,可以吗?

go - 将内容 append 到特定部分中的 Go 模板

Golang 项目 Travis CI Build 失败,错误为 `Makefile:15: recipe for target ' test' failed`

go - 在 golang 上使用接口(interface)和类型

variables - golang 变量替换

elasticsearch - Go弹性客户端: have to wait some time before run query

GO - 文件包名称