Featured image of post Go Basic Syntax Operation

Go Basic Syntax Operation

Learn basic Golang module,

back to Go tutorial index

Intro

This article will cover some basic theory and operation of Go programming language:

  • Project Structure
  • Working with variable and constant
  • Working with Functions
  • Working with Conditionals
  • Working with Loops
  • Array and Slices
  • Working with Maps
  • Working with Struct
  • Concurency in Go

Concurrency Model

Go’s concurrency model

  • Actor
  • Communicating Sequential Processes (CSP)

Sample Go Routine

Channels

There are two types of channels: Unbuffered (syncronous behaviour) and buffered. The unbuffered can’t hold data. So any goroutine putting data onto one blocks until there’s a receiver on the other end. Well, on the other hand, buffered channels, they can hold data, so a Goroutine can drop data onto it and crack straight on with whatever it was doing without having to care if there’s a receiver on the other end.

To create an unbuffered channel, we can use make with the chan keyword, e.g. myChann := make(chan int). And then, obviously, Go wants to know the type of data the channel is going to hold. But to make it buffered, all we do is add buffers. So this one here will have 5, myChann := make(chan int, 5), and because it’s for int, it means it can hold up to 5 ints.

The effect of a buffered channel is that goroutines using it don’t need to block. In fact, actually, as long as the channel is not full, other goroutines can come along after and can also put data on it. So proper asynchronous behavior. Now, obviously, if buffered channel is full, then any goroutines wanting to use it are going to block until it frees up. And then, likewise, if a receiver is trying to grab data off of a channel and the data’s not there yet, well that blocks until it is.

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy