When are two channels equal?

156 views
Skip to first unread message

Jason E. Aten

unread,
Jan 22, 2025, 2:00:44 AMJan 22
to golang-nuts
come across the interesting but a little ambiguous definition
of equality of channels:

"Channel types are comparable. Two channel values are
equal if they were created by the same call to make or
if both have value nil." (emphasis mine)


I tried the simplest/obvious (to me) interpretation that "the same make"
meant textually the same file and line number. But no, it appears not:



package main

import "fmt"

func main() {

a := make(chan bool) // created by the make of line 22
b := make(chan bool) // created by the make of line 23

fmt.Printf(" is a == b ? %v \n", a == b) // false. "makes" sense (ha!), as line 22 != line 23.

var slc []chan bool
for i := range 3 {
slc = append(slc, make(chan bool)) // all three are created by the make of line 29.
if i > 0 {
fmt.Printf("is slc[%v] == slc[0] ? %v \n", i, slc[i] == slc[0])
// always says false, even though all 3 were from "the same make", that of line 29.
}
}
}


Output:
 is a == b ? false
is slc[1] == slc[0] ? false
is slc[2] == slc[0] ? false 

So what is "the same make"?

Dan Kortschak

unread,
Jan 22, 2025, 2:10:10 AMJan 22
to golan...@googlegroups.com
On Tue, 2025-01-21 at 23:00 -0800, Jason E. Aten wrote:
>
> So what is "the same make"?

It's not the same make, it's the same *call* to make. So a := make(chan
struct{}); b := a; if a == b { /* this executes */ }.

Jason E. Aten

unread,
Jan 22, 2025, 2:13:21 AMJan 22
to golang-nuts
Thanks Dan.

Also I was mixing up "comparable" and "actually being equal", as in == returning true.
I think comparable just means "will compile without error".

Dan Kortschak

unread,
Jan 22, 2025, 2:19:30 AMJan 22
to golan...@googlegroups.com
On Tue, 2025-01-21 at 23:13 -0800, Jason E. Aten wrote:
> I think comparable just means "will compile without error".

Yes, this is the case.

Reply all
Reply to author
Forward
0 new messages