Vinícius Krolow

Let's hurt the code!

Fri, Feb 26, 2016

Introduction to go channels

Topic: go Learning Path: Channel

Post: Introduction To Golang: Channels

In this post @karlseguin explains how channels work in golang.

So basically channels is the way golang uses to make communication between goroutines. Can be for both, pass signals or data around, different than thread-based programming it is not necessary to lock data around, to avoid deadlocks, as with goroutines only one goroutine has access to a piece of data at a given time. So it does not do a lock of data.

He gives one analogy about, thread vs golang channels:

Consider a phone conversation. It can be difficult to coordinate who should talk, especially as more people get added to the conversation. It isn't uncommon for two people to speak at once. Conversely, it also isn't uncommon for one person to do all the talking and not let the others participate. This, to me, is what thread-based programming is like.

Channels on the other hand, are more like passing notes in class. Coordination is strictly controlled by the act of passing the note to someone. There's simply no way for you and your friend to write on the same note at the same time.

How create a go channel?

c := make(chan int, 1)

Basically it creates one channel that enables pass integers between goroutines. It is called buffer channel.

Hey, have you find a typo or english smell? help me improve my english, i'll be glad to receive an improvement here.