Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the channel type have a '<-' in it?

Tags:

go

This works.

var tick <-chan time.Time = time.Tick(1e8)

However, this does not.

var tick chan time.Time = time.Tick(1e8)

Why do I need a <- in my type declaration for a channel? I thought that <- was for writing to or reading from a channel. Why would it appear in a type?

like image 863
dangerChihuahua007 Avatar asked Dec 27 '12 21:12

dangerChihuahua007


People also ask

What is YouTube channel type?

Educational: These channels inform and educate with videos such as documentaries. Entertainment: These channels feature musicians, comedians, animators, and other entertainers sharing creative skills. Gaming: One of the most popular channel types, gaming channels produce videos related to video and tabletop games.


2 Answers

Channels can have a type indicating whether it is readonly, writeonly or both.

Indicating a channel direction is done with <- as part of the type or omitted for a read/write channel.

So the <- in <-chan time.Time is part of the type,

chan   time.Time  //Would be a read/writable channel
chan<- time.Time  // Would be a write only channel
<-chan time.Time  // Would be a read only channel

and time.Tick(1e8) returns a read only channel.

Read more in the language spec here

like image 181
nos Avatar answered Oct 05 '22 12:10

nos


A good way to think of channels is as pipes with two ends. One end is where the events flow in and the other where they flow out. So declaring a channel, e.g.

var c = make(chan int)

creates a channel as a whole - i.e. with both ends. Conversely,

func consume(c <-chan int) {
    ...
}

defines a function with a channel input parameter - i.e. the readable end of a channel, and

func generate(c chan<- int) {
    ...
}

defines a function with a channel output parameter - i.e. the writable end of a channel. Both these functions can have the whole channel passed as their actual parameter, or just the end they need.

The same general principle applies when channels are used as local variables or as fields in structs.

It's good practice to use the channel-end syntax wherever possible, because the compiler will be able to check more thoroughly that you've written what you intended.

Interestingly, the occam programming language also has equivalent syntax to mark which end of a channel is which.

like image 21
Rick-777 Avatar answered Oct 05 '22 13:10

Rick-777