Let’s Go ~ A Complete Guide (Part ~ VII)

Himanshu Singh
MindOrks
Published in
3 min readAug 9, 2018

This is the Seventh part of the series Let’s Go ~ A Complete Guide.If you have not followed it yet click here, I would request you to go and get an idea why should we learn GoLang.Following is the content of the Series,

  1. Introduction to Go
  2. Go Environment Setup
  3. Go Basics
  4. Control Statement and Functions
  5. Structs
  6. Object Oriented Programming in Go
  7. Concurrency

Concurrency

  • Go Language is a simple language just like C Language
  • It supports Concurrency at the language level itself.
  • Go uses a different approach in which shared values are passed around on channels and, in fact, never actively shared by separate threads of execution

So before that, Lets understand the term Concurrency

What is Concurrency?
Concurrency is the ability of an algorithm or program to run more than one task at a time.

How Concurrency is useful in today’s world?
Concurrency is being able to run multiple tasks in parallel, which can increase the efficiency of an application.
In practical world, if the efficiency increases the cost decreases of the system. The time to do any task also decrease. So can we say it is fast?

Let’s Get started with Co-Concurrency.

go-routines

  • A goroutine is a lightweight thread of execution.
  • They are almost same as a thread in other programming languages like java.
  • Goroutines run in the same address space.
  • Goroutines are function or methods that run concurrently with other functions or methods
  • One goroutine usually uses 4~5 KB of stack memory. So, we can run multiple number of goroutines in a single system.
  • goroutines run on the thread manager at runtime.
  • We use the go keyword to initialise the goroutine in a program under the main function.

Channels

  • Channels are a typed conduit through which you can send and receive values with the channel operator, <-
  • It is a very good communication medium in Go between two different go-rountine
  • It is a two way communication process to send and receive data between two different gorountine.
  • We use make keyword to create a new channel
  • To understand Channel in a simple way, lets consider an example of a telephone. It is a communication system between two people where voice is transferred to and fro.Channel works in a similar way

Example,

ci := make(chan int)
cs := make(chan string)

channel uses the operator <- to send or receive data.

ch <- v    // send v to channel ch.
v := <-ch // receive data from ch, and assign to v
  • By default, sends and receives block wait until the other side is ready to emit and receive the data.

Buffered channels

  • Above was an example of non-buffered channel
  • It takes additional parameter to create a channel.It is called as buffer length
  • To understand Buffer Channels better, consider it as array where you have to define the size of the array while creating it.

Syntax,

channelName := make(chan dataType,length)
  • So, we can send element upto the specified length and if we try to send more then the specified length it would throw an error,
fatal error: all goroutines are asleep - deadlock!

Range and Close

  • A sender can close a channel to indicate that no more values will be sent
  • Receivers can test whether a channel has been closed by assigning a second parameter to the receive expression.We can use range to operate on buffer channels as in slice and map,
  • Only the sender should close a channel, never the receiver
  • You don’t have to close them frequently unless you are sure the channel is completely useless, or you want to exit range loops.
  • Range can be used in Slice, Maps

Miscs

  • Go can also listen to multiple channel at once using select keyword.
  • We can also use goroutines using Runtime

That’s it for the series in Go. I have tried to minimise it and make it simpler for getting started to learn Go. I have followed some examples from online samples(for demonstration purpose).

If you are followed this series till the end, add 👏 and show some love. 💌

Cheers.
Thank you for bearing with me so long.Follow me on Facebook, Twitter, Instagram 😃. Also follow Mindorks Community online and learn from its resources. We would be coming up with new tutorial on backend programming with Go

--

--