Basics

Notes on the GO Programming language

Updated: 03 September 2023

Workspace

Go is very specific in the way we set up our folders by making use of a specific structure for our workspace folders

A workspace should have a bin and src folder in which our project code will reside, we typically use something like the following

1
go (workspace)
2
- bin
3
- src
4
-github.com
5
-username
6
- project1
7
# project files
8
- project2
9
# project files
10
- pkg

We need to set the GOPATH environment variable to the folder in which we want our workspace to be

In my case I’m using C:\repos\go

Next make the necessary directories

Install Package

Install a package with the go get command, for example the aws package:

Terminal window
1
go get github.com/aws/aws-sdk-go/aws

Hello World

Make a new project directory with a file called main.go with the following content

Go has a main function that needs to be created for every package and it will be run automatically. We also need to import fmt to print content to the screen. Also note that Go strings must have double quotes "hello" or we will get an error

1
package main
2
3
import "fmt"
4
5
func main() {
6
fmt.Println("Hello World)
7
}

We can run this with the following command from the project dir

Terminal window
1
go run main.go

Build and Run

We can build binaries for an application with go install which can then be executed from the terminal

Build and run the package with the following command from the project directory

Terminal window
1
go install

And then from the bin directory

Terminal window
1
./01_hello.exe

Variables and Data Types

Go has the following data types

  • string
  • bool
  • int int8 int16 int32 int64
  • uint uint8 uint16 uint32 uint64
  • byte (uint8)
  • rune (int32)
  • float32 float64
  • complex64 complex128

There are a few different ways to create variables, note that if we create a variable and do not use it we will get an error

Var

1
var name string = "John"

Type Inference

1
var name = "John"

Get Type

We can make use of the following function to get the type of a variable

1
fmt.Printf("%T",name)

Constants

We can define constants with the const keyword

1
const name = "John"

Global Variables

We can declare global variables by defining them outside of the main function

Shorthand Method

Inside of a function we can declare variables using the assignment operator with the following

1
name := "John

Multiple Assignments

We can do multiple assignments as follows

1
name, age := "John", 15

Packages

Importing Packages

We can import multiple packages by declaring each package in the import statement on a different line

1
import (
2
"fmt"
3
"math"
4
)

Creating Packages

Create a new folder and in it you can define the package name and some functions in it

1
package mypackage
2
3
func myfunction() {
4
...
5
}

And then import the package by referring to its path in the import function

Functions

Functions are defined with the func keyword, the function name, inputs, and return types and values

1
func greet(name string) string {
2
return "Hello " + name
3
}

Arrays and Slices

In Go arrays are fixed length, and a slice is an array without a fixed size

Arrays

Arrays are fixed in size and can be defined with

1
var myArray [3]string
2
3
myArray[0] = "Hello"
4
myArray[2] = "Word"

Or with initial values

1
myArr := [2]string{"Hello", "World"}

Slices

A slice is essentially an array that does not have a fixed size

1
mySlice := []string{"Hello", "World","!"}

We can also make slices by using the same notation as other languages

1
newSlice := mySlice[2:5]

Conditionals

Conditionals do not require parenthesis, however they can be used

If Else

1
if x < y {
2
...
3
} else if x == y {
4
...
5
} else {
6
...
7
}

Swtich/Case

1
switch x {
2
case: 5:
3
...
4
case 10:
5
...
6
default:
7
...
8
}

Loops

There are two methods for building for loops

1
i := 1
2
for i <= 01 {
3
...
4
i++
5
}
1
for i := 1; i <= 10; i++ {
2
...
3
}

Maps

Maps are key-value pairs and can be defined and accessed with

1
ages := make(map[string]int)
2
3
ages["Bob"] = 35
4
ages["John"] = 5

We can delete a value from a map with the delete function

1
delete(emails, "Bob")

We can also declare a map with initial values

1
emails := map[string]int{"Bob":35, "John":5}

Range

Range is used for looping through values

1
ids := []int{1,2,3}
2
3
for i, id := range ids{
4
...
5
}

If we are not using the i variable, we can use an _ to receive any inputs that we will not use

1
for _, id := range ids{
2
...
3
}
1
emails := map[string]int{"Bob":35, "John":5}
2
3
for k, v := range emails {
4
...
5
}

Pointers

A pointer allows us to point to the memory address of a specific value, we can get the pointer for a value with the & sign

1
a := 5
2
b := &a

If we wan to get back from the pointer to the actual value we can do that with the * operator

1
a == *b // true
2
a == *&a //true

We can modify the value of a pointer

1
*b = 10
2
a == 10 // true

The reason to use pointers can be more efficient when passing values

Closures

We can define anonymous functions to declare anonymous functions that can be used as closures

1
func adder () func (int) int {
2
sum := 0
3
return func(x int) int {
4
sum += x
5
return sum
6
}
7
}
8
9
10
func main() {
11
sum := adder()
12
for i:= 0; i < 10; i++ {
13
fmt.Println(sum(i)) // 0 1 3 6 ...
14
}
15
}

Structs

Structs are like classes

Structs can contain values and functions, of which we can have value reveivers and pointer receivers. Value receivers just do calculations, Pointer Receivers modify data

1
type Person struct {
2
firstName string
3
lastName string
4
age int
5
}
6
7
func (p Person) valueReceiver() string {
8
return "Hello " + p.firstName
9
}
10
11
func (p *Person) pointerReceiver() {
12
p.age++
13
}
14
15
func main() {
16
person := Person(firstName: "John", lastName: "Smith", age: 25)
17
person2 := Person("John", "Smith", 25)
18
19
person.firstName // John
20
21
person.valueReceiver() // Hello John
22
person.pointerReceiver() // person.age = 26
23
}

Interfaces

1
type Shape interface {
2
area() float64
3
}
4
5
type Circle struct {
6
x, y, radius float64
7
}
8
9
type Rectangle struct {
10
width, height float64
11
}
12
13
func (c Circle) area() float64 {
14
return math.Pi * c.radius * c.radius
15
}
16
17
func (r Rectangle) area() float64 {
18
return r.width * r.height
19
}
20
21
func getArea(s Shape) float64 {
22
return s.area()
23
}

Web

To work with HTTP requests we can use thenet/http package which allows us to define a function to handle a specific route

1
package main
2
3
import ("fmt"
4
"net/http"
5
)
6
7
func main(){
8
http.HandleFunc("/", index)
9
fmt.Println("Server Starting")
10
http.ListenAndServe(":3000", nil)
11
}
12
13
func index(w http.ResponseWriter, r *http.Request) {
14
fmt.Fprintf(w, "Hello World")
15
}