Why comparison between two `*bytes.Buffer` type variables showing they are not equal even though they are same?

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

Why comparison between two `*bytes.Buffer` type variables showing they are not equal even though they are same?

问题

这是问题的代码。比较两种缓冲区类型显示它们不相等,但两个*File类型是相等的。

func main() {
    var v, w io.Writer
	v := &bytes.Buffer{}
	w := &bytes.Buffer{}
	v.Write([]byte("Hello"))
	w.Write([]byte("Hello"))
	fmt.Println(v == w) // false

    v := os.Stdout
    w := os.Stdout
    v.Write([]byte("Hello"))
	w.Write([]byte("Hello"))
	fmt.Println(v == w) // true

}

以下是问题的翻译结果:

这是问题的代码。比较两种缓冲区类型显示它们不相等,但两个*File类型是相等的。

func main() {
    var v, w io.Writer
	v := &bytes.Buffer{}
	w := &bytes.Buffer{}
	v.Write([]byte("Hello"))
	w.Write([]byte("Hello"))
	fmt.Println(v == w) // false

    v := os.Stdout
    w := os.Stdout
    v.Write([]byte("Hello"))
	w.Write([]byte("Hello"))
	fmt.Println(v == w) // true

}
英文:

Here is the code for the problem. comparing the two buffer types shows they are not equal but two *File types are equal.

func main() {
    var v, w io.Writer
	v := &bytes.Buffer{}
	w := &bytes.Buffer{}
	v.Write([]byte("Hello"))
	w.Write([]byte("Hello"))
	fmt.Println(v == w) // false

    v := os.Stdout
    w := os.Stdout
    v.Write([]byte("Hello"))
	w.Write([]byte("Hello"))
	fmt.Println(v == w) // true

}

答案1

得分: 5

你正在比较指针,而不是对象。表达式&bytes.Buffer{}在内存中创建一个新对象并返回指向它的指针。这样做两次会得到两个不同的指针,因为没有两个对象可以驻留在同一内存位置。

要比较缓冲区的实际内容,可以使用类似bytes.Compare(v.Bytes(), w.Bytes())的方法。

v := &bytes.Buffer{}
w := &bytes.Buffer{}
v.Write([]byte("Hello"))
w.Write([]byte("Hello"))
fmt.Printf("%p, %p\n", v, w)                     // 打印两个不同的值
fmt.Println(bytes.Compare(v.Bytes(), w.Bytes())) // 0,表示“相等”

关于第二个案例,os.Stdout是一个包含指向os.File的指针的全局变量,因此该代码片段比较了指向同一对象的两个指针。

v := os.Stdout
w := os.Stdout
v.Write([]byte("Hello"))
w.Write([]byte("Blah!")) // 不重要
fmt.Println(v == w) // true:v 和 w 指向同一对象!
英文:

You are comparing pointers, not objects. The expression &bytes.Buffer{} creates a new object in memory and returns a pointer to it. Doing that twice would give two different pointers, since no two objects can reside at the same memory location.

To compare the actual contents of buffers, use something like bytes.Compare(v.Bytes(), w.Bytes())

	v := &bytes.Buffer{}
	w := &bytes.Buffer{}
	v.Write([]byte("Hello"))
	w.Write([]byte("Hello"))
	fmt.Printf("%p, %p\n", v, w)                     // prints 2 different values
	fmt.Println(bytes.Compare(v.Bytes(), w.Bytes())) // 0, means "equal"

Regarding the second case, os.Stdout is a global variable containing a pointer to os.File, so the snippet compares two pointers to the same object.

	v := os.Stdout
	w := os.Stdout
	v.Write([]byte("Hello"))
	w.Write([]byte("Blah!")) // doesn't matter
	fmt.Println(v == w) // true: v and w point to the same object!

huangapple
  • 本文由 发表于 2022年1月12日 22:28:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/70683299.html
匿名

发表评论

匿名网友

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

确定