Introduction to GO Programming language

Basic variations in go language for programmer who are familiar with other languages.


Var X(name) String(type)
X = "First"
Go Compiler can infer the type when variable assigned with ':'
X := "First"

Consts: (variables whose values can not be changed)
const x string;

Multible variables- define as follows with each variable in its own line.
var (
a = 5
b = 10
c = 15


var x [5]int : x is an array composed of five elements:

for _, value := range x (Addding all the values in array)
{ total += value }

'_' specify the compiler iterator variable is not required in this case.

Go compiler supports shorter forms can infer type if not specified.

x := [5]float64{ 98, 93, 77, 82, 83 }


Slices is segments in array with variable length.

x := make([]float64, 5) ( use built in make to create and associate with an array of len 5)

arr := []float64{1,2,3,4,5}
x := arr[0:5] [ slice from 0 to 5 in array]

Avaiable functions on slices - Copy append etc..


Maps are unordered collection of key value pairs.
var x map[string]int

maps needs to be initialized before they can use.
x := make(map[string]int)
x["key"] = 10

Accessing map return two values, ok is true if its successfull .
if name, ok := elements["Un"]; ok { fmt.Println(name, ok) }

More complex maps can be built. E.g::Map of strings of map of strings of strings

elements := map[string]map[string]string{ "H": map[string]string{ "name":"Hydrogen", "state":"gas", }

Syntax for function that takes slice of float and return float
func average(xs []float64) float64 { panic("Not Implemented") }

func add(args int { },in this case it takes zero or more parameters of type int

Defer,Panic & Recover::

f, _ := os.Open(filename) defer f.Close()
defer will run the function at end of the programme.

defer func() { str := recover() fmt.Println(str) }()

pair of defer and panic, the defer function runs after the panic.

Structs & Pointers

type Circle struct { x, y, r float64 }

func circleArea(c Circle) float64 { return math.Pi * c.rc.r }

Methods: we can declare the same functions as
func (c Circle) area() float64 { return math.Pi * c.rc.r }

By creating the function in this way it allows us to call the function using the . operator kike c.area().This is much easier to read, we no longer need the & operator (Go automatically knows to pass a pointer to the circle for this method) and because this function can only be used with Circles we can rename the function to just area.

type Shape interface { area() float64 }

Like a struct an interface is created using the type keyword, followed by a name and the keyword interface. But instead of defining fields, we define a “method set”


A goroutine is a function that is capable of running concurrently with other functions

Channels provide a way for two goroutines to communicate with one another and synchronize their execution

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)

We can specify a direction on a channel type thus restricting it to either sending or receiving

Go has a special statement called select which works like a switch but for channels

It's also possible to pass a second parameter to the make function when creating a channel.This creates a buffered channel with a capacity of 1. Normally channels are synchronous; both sides of the channel will wait until the other side is ready. A buffered channel is asynchronous; sending or receiving a message will not wait unless the channel is already full.