Connect with us

Programming Languages

Concurrency in go Programming Language?

Concurrency in go Programming Language? Goroutines In Go , Channels In Go

funkyprogrammer_99902

Published

on

Large programs are often made up of many smaller sub-programs. For example, a web server handles requests made from web browsers and serves up HTML web pages in response. Each request is handled like a small program.

If you want to learn Go programming language click on Enroll Now button

Concurrency can be notoriously difficult to get right, but fortunately, the Go open source programming language makes working with concurrency tractable and even easy. If you’re a developer familiar with Go, this practical book demonstrates best practices and patterns to help you incorporate concurrency into your systems.

It would be ideal for programs like these to be able to run their smaller components at the same time (in the case of the webserver to handle multiple requests). Making progress on more than one task simultaneously is known as concurrency. Go has rich support for concurrency using goroutines and channels.

Goroutines In Go

A goroutine is a function that is capable of running concurrently with other functions. To create a goroutine we use the keyword go followed by a function invocation:

package main

import "fmt"

func f(n int) {
  for i := 0; i < 10; i++ {
    fmt.Println(n, ":", i)
  }
}

func main() {
  go f(0)
  var input string
  fmt.Scanln(&input)
}

This program consists of two goroutines. The first goroutine is implicit and is the main function itself. The second goroutine is created when we call go f(0). Normally when we invoke a function our program will execute all the statements in a function and then return to the next line following the invocation. With a goroutine we return immediately to the next line and don’t wait for the function to complete. This is why the call to the Scanln function has been included; without it the program would exit before being given the opportunity to print all the numbers.

  • Understand how Go addresses fundamental problems that make concurrency difficult to do correctly
  • Learn the key differences between concurrency and parallelism
  • Dig into the syntax of Go’s memory synchronization primitives
  • Form patterns with these primitives to write maintainable concurrent code
  • Compose patterns into a series of practices that enable you to write large, distributed systems that scale
  • Learn the sophistication behind goroutines and how Go’s runtime stitches everything together

Channels In Go

Channels provide a way for two goroutines to communicate with one another and synchronize their execution. Here is an example program using channels:

package main

import (
  "fmt"
  "time"
)

func pinger(c chan string) {
  for i := 0; ; i++ {
    c <- "ping"
  }
}

func printer(c chan string) {
  for {
    msg := <- c
    fmt.Println(msg)
    time.Sleep(time.Second * 1)
  }
}

func main() {
  var c chan string = make(chan string)

  go pinger(c)
  go printer(c)

  var input string
  fmt.Scanln(&input)
}

This program will print “ping” forever (hit enter to stop it). A channel type is represented with the keyword chan followed by the type of the things that are passed on the channel (in this case we are passing strings). The <- (left arrow) operator is used to send and receive messages on the channel. c <- "ping" means send "ping"msg := <- c means receive a message and store it in msg. The fmt line could also have been written like this: fmt.Println(<-c) in which case we could remove the previous line.

Problems

  • How do you specify the direction of a channel type?
  • Write your own Sleep function using time.After.
  • What is a buffered channel? How would you create one with a capacity of 20?

If you want to learn Go programming language click on Enroll Now button

Copyright © 2021 By Funky Programmer.