Pages

Wednesday, 9 November 2022

Why golang chose left arrow <- rather than right arrow -> as chan operator?

 


In Go, we read from and write to a chan by operator left arrow as below.

chan1 := make(chan string, 2)

chan1 <- "hello"  // write to chan
s2 := <- chan1    // read from chan 

You may wonder why Go use <- instead of -> as chan operator.

I haven't seen any official explanation yet. So below is just my personal opinion.

The reason is in programming world, most languages treat the left hand side as destination while the right hand side as source, e.g.

MOV A, B  # assebly language to move B to A
x = y;    // C/Python/Java/... to assign y's value to x

So, Go follows the same pattern and use left arrow to read/write chans.


No comments:

Post a Comment