Lets catch your dream

Get up and catch your dreams. “Lets catch your dream” is published by Alirezaemami.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




An introduction to using channels in GO and how they can be useful in goroutines

Go

Go is very addictive and easy to use language GO is primarily used for writing a high-class application that can handle thousands of requests per second.

At first, everything about the Go is easy to understand but a lot of the time, programs written in Go are highly confusing because of the concurrency and asynchronous nature of the code. The main challenge is to understand what is a channel and how it can be used with goroutines. The fact that not many other popular programming languages have a similar concept, means that channels are one concept that you have to spend some time learning them if you’re starting your journey with Go.

To understand to channels first let us understand the goroutines first

The problems with this implementation, is that these two parts of our code are rather disconnected. As a consequence :

Now here comes the saviour (Channels in Go)
A channel gives us a way to “connect” the different concurrent parts of our program. In this case, we can connect main() and addTwo()

Channels can be thought of as “pipes” that connect the different parts of our code.

You can also declare a channel without giving directionality, which means it can send or receive data:

The <-out statement will block the code until some data is received on the out channel. and fmt.Println(<-out) will print data on screen and exit the main block.

This is it we are done with simplest channel explanation on Go. Channels in Go make concurrent programming much easier than it would be without them, and it's hard to appreciate the amount of code that we don't have to write because of them.

Add a comment

Related posts:

5 Things NOT To Do If You Want To Be Successful In Your Business

There is so much speculation for what practices carry you through to success in today’s day and age. Taken from my own experiences, I am going to tell you the exact way NOT to do things if being…

What is the current stage of Enterprise Blockchain?

What is the current stage of Enterprise Blockchain?. I hope you enjoyed last week’s article where I wrote about the challenges to engage multiple parties in a blockchain project….

Automating Social Media

One of my biggest takeaways from my time at CEX was that I should really be on Twitter, being in the niche I’m in. Naturally, one of the first things I wanted to look into was how I can easily…