Go

Go (also known as Golang) is a powerful and statically-typed programming language designed for efficiency and scalability. Developed by Google, Go is known for its simplicity, readability, and fast execution speed. It offers built-in support for concurrent programming, allowing developers to easily create efficient and scalable applications. With its strong static typing and efficient garbage collection, Go is ideal for building high-performance systems and distributed applications.

Module 1: Introduction to Go

Introduction to Go

Go, also known as Golang, is a statically typed, compiled language designed at Google. It's known for its simplicity and efficiency in producing robust, reliable, and efficient software.

Variables, Types and Control Flow

Go is a statically typed language which supports a variety of types including integer types, floating point numbers, strings, etc. Go has several control flow statements including 'if', 'for', 'switch', and 'select'.

package main

import 'fmt'

func main() {
	var name string = 'Go'
	fmt.Println(name)
}

Functions

Functions in Go are defined using the 'func' keyword. Go supports multiple return values from functions.

package main

import 'fmt'

func greetings(name string) string {
	return 'Hello, ' + name
}

func main() {
	fmt.Println(greetings('Go'))
}

Module 2: Structs and Interfaces in Go

Structs

A struct is a collection of fields, and is Go's replacement for classes. You can access the fields in a struct using a dot.

package main

import 'fmt'

type Person struct {
	name string
	age  int
}

func main() {
	var p Person
	p.name = 'Bob'
	p.age = 20
	fmt.Println(p)
}

Interfaces

An interface is a collection of method signatures. It provides a way to specify the behavior of an object. If something can do 'this', then it can be used 'here'.

package main

import 'fmt'

type Shape interface {
	Area() float64
}

type Circle struct {
	radius float64
}

func (c Circle) Area() float64 {
	return 3.14 * c.radius * c.radius
}

func printArea(s Shape) {
	fmt.Println(s.Area())
}

func main() {
	circle := Circle{5}
	printArea(circle)
}

Module 3: Concurrency in Go

Goroutines

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'
	'time'
)

func say(s string) {
	for i := 0; i < 3; i++ {
		time.Sleep(100 * time.Millisecond)
		fmt.Println(s)
	}
}

func main() {
	go say('world')
	say('hello')
}

Channels

Channels are a typed conduit through which you can send and receive values with the channel operator, '<-'.

package main

import 'fmt'

func main() {
	messages := make(chan string)
	go func() { messages <- 'ping' }()
	msg := <-messages
	fmt.Println(msg)
}

Module 4: Error Handling in Go

Error Handling

In Go it’s idiomatic to communicate errors via an explicit, separate return value. This differs from the exceptions used in languages like Java and Ruby and the overloaded single result / error value sometimes used in C.

package main

import 'errors'
import 'fmt'

func f1(arg int) (int, error) {
	if arg == 42 {
		return -1, errors.New('can't work with 42')
	}
	return arg + 3, nil
}

func main() {
	for _, i := range []int{7, 42} {
		if r, e := f1(i); e != nil {
			fmt.Println('f1 failed:', e)
		} else {
			fmt.Println('f1 worked:', r)
		}
	}
}

Module 5: Testing in Go

Writing and Running Tests

Go has a built-in testing tool called 'go test'. You can write a test for a function by creating a file with a name ending in '_test.go' that contains functions named 'TestXXX' with signature func (t *testing.T).

package main

import 'testing'

func TestSum(t *testing.T) {
	total := Sum(5, 5)
	if total != 10 {
		t.Errorf('Sum was incorrect, got: %d, want: %d.', total, 10)
	}
}