在Go语言中比较指针

huangapple go评论90阅读模式
英文:

Comparing pointers in Go

问题

我正在阅读我的Go书,它说指针是可比较的。它说:如果两个指针指向同一个变量或者都是nil,那么它们是相等的。

那么为什么我的下面的代码在比较两个指向不同变量的指针时打印出'true'呢?

func main() {
	var p = f()
	var q = f2()
	fmt.Println(*p == *q) // 为什么是true?
}

func f() *int {
	v := 1
	return &v
}

func f2() *int {
	w := 1
	return &w
}
英文:

I am reading in my Go book that pointers are comparable. It says: two pointers are equal if and only if they Point to the same variable or both are nil.

So why is my following code printing 'true' when comparing two pointers which are pointing to two different variables?

func main() {
	var p = f()
	var q = f2()
	fmt.Println(*p == *q) // why true?
}

func f() *int {
	v := 1
	return &v
}

func f2() *int {
	w := 1
	return &w
}

答案1

得分: 14

你没有比较指针本身,因为你使用了“解引用运算符”*,它返回存储在该地址上的值。在你的示例代码中,你调用了返回两个不同指针的方法。存储在这些不同地址上的值恰好都是1。当你解引用指针时,你获取到存储在那里的值,所以你只是在比较1 == 1,这是真的。

如果比较指针本身,你会得到false;

package main

import "fmt"

func main() {
    var p = f()
    var q = f2()
    fmt.Println(*p == *q) // 为什么是true?

    fmt.Println(p == q) // 指针比较,比较存储的内存地址值
    // 而不是该地址值上的值

    // 查看你实际获取到的内容
    fmt.Println(p) // 这里是十六进制地址值
    fmt.Println(q)
    fmt.Println(*p) // 1
    fmt.Println(*q) // 1
}

func f() *int {
    v := 1
    return &v
}

func f2() *int {
    w := 1
    return &w
}

你可以在这里查看代码:https://play.golang.org/p/j2FCGHrp18

英文:

You aren't comparing the pointers themselves because you use the 'dereference operator' * which returns the value stored at that address. In your example code, you've called the methods which returned two different pointers. The value stored at each of those different addresses happens to be 1. When you derefernce the pointer you get the value stored there so you're just comparing 1 == 1 which is true.

Comparing the pointers themselves you get false;

package main

import "fmt"

func main() {
    var p = f()
    var q = f2()
    fmt.Println(*p == *q) // why true?

    fmt.Println(p == q) // pointer comparison, compares the memory address value stored
    // rather than the the value which resides at that address value
    
    // check out what you're actually getting
    fmt.Println(p) // hex address values here
    fmt.Println(q)
    fmt.Println(*p) // 1
    fmt.Println(*q) // 1
}

func f() *int {
    v := 1
    return &v
}

func f2() *int {
    w := 1
    return &w
}

https://play.golang.org/p/j2FCGHrp18

答案2

得分: 3

package main

import "fmt"

func main() {
    var p = f()
    var q = f2()

    fmt.Println(*p == *q)
    /* 是真的,因为 *p = *q = 1 */

    fmt.Println(p == q)
    /* 是假的,因为 *p 和 *q 存储了两个不同的内存地址 */
}

func f() *int {
    v := 1
    return &v
}

func f2() *int {
    w := 1
    return &w
}

你可以在这个链接中查看代码:https://play.golang.org/p/i1tK4hhjOf

英文:
package main

import "fmt"

func main() {
    var p = f()
    var q = f2()

    fmt.Println(*p == *q)
    /* is true, since *p = *q = 1 */

    fmt.Println(p == q)
    /* is false, since *p and *q store two different memory addresses */
}

func f() *int {
    v := 1
    return &v
}

func f2() *int {
    w := 1
    return &w
}

https://play.golang.org/p/i1tK4hhjOf

huangapple
  • 本文由 发表于 2015年12月15日 04:41:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/34276205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定