Go: A simple concurrent Go program using channels and goroutines

· Tech

Go is a language popular for its inbuilt concurrency support. Concurrency is important these days especially because the hardwares are hitting theoretical limit. Therefore developers need to be able to utilise the resources the best way possible. This means a core in the computer needs to juggle between multiple things but ONLY one thing at a time. This is a fundamental difference between concurrency and parallelism. This article shows Go concurrency by explaining a code which uses goroutine and channels to concurrently get 3 tasks done. (Subscribe to my channel to get regular videos on Go)

The code below does 3 things:

  1. Wait for a user to enter something (Input() function)
  2. THEN convert it to a hash (which is an encrypted format of what was entered) (Hash() function)
  3. THEN print the output (Output() function)

This is accomplished by using goroutines and channels.

We have functions called Input(), Hash() & Output() to accomplish these tasks. We start them in goroutines and “connect” them using channels. The Input() function has an infinite for loop which listens for a user input. As soon as it gets an input, it sends it to the channel called hashChan.
Inside Hash() function there is the same channel hashChan which is waiting for anything to be sent into it inside a for loop:

ss := <-hashChan

The above line won’t get executed until there is something sent into the channel hashChan. This is the way channels in Go are designed. This can be changed by making the channel buffered but that’s outside of current scope.

Once the variable ss receives something through hashChan channel it can carry on the hashing and send the result to the outputChan which is “waiting” to receive something in the Output() function. And just like in the case of hashChan this gets executed afterwards to print the hash.

// Input -> Hash -> Output
// This program demonstrates concurrency using channels in Go
// The program waits for a user input in an infinite for loop
// When it gets an input,
//    the string input gets hashed in a function through a channel(hashChan)
// After it gets hashed,
//    it gets printed, again using a channel (outputChan)
package main

import (
“crypto/sha256”
“fmt”
)

func main() {
hashChan := make(chan string)
outputChan := make(chan string)

go Input(hashChan, outputChan)
go Hash(hashChan, outputChan)
go Output(outputChan)
for {
// Use infinite for loop or use sync
// to wait for the goroutines as long as you want
}
}

func Input(hashChan chan string, outputChan chan string) {
var s string
for {
fmt.Scanf(“%s”, &s)
hashChan <- s
}
}

func Hash(hashChan chan string, outputChan chan string) string {
for {
ss := <-hashChan
hash := sha256.Sum256([]byte(ss))
outputChan <- string(hash[:])
}
}

func Output(outputChan chan string) {
for {
s := <-outputChan
fmt.Println(s)
}
}

If you want to learn more about Go in the context of gRPC then check out this FREE course: