GoLang

Introduction

Purpose:

Install:

When you first have a project, you do go mod init [name]. This will create a file like the following:

module test

go 1.16

And in main.go, we write

package main // which package this file belong to

import "fmt" // import package

func main() {
  fmt.Print("Hello World")
}

To execute, we do go run main.go

Go Syntax

For more, see Test

Next Step

Here are some useful links:

Warnings

Note that when printing pointers with fmt library, it might not print the value of pointer (ie. the address) but it might print what the pointer is pointing to (ie. the struct). Consider the following code:

type Stuff struct {
    X int
}

func main() {
    p := &Stuff{1}
    fmt.Println(p)
}

It will print out &{1}. This is because fmt.Println(p) is the same as fmt.Printf("%v\n", p).

To print the address, you can do

p := &Stuff{1}
fmt.Printf("%p\n", p)

You can run go with go -race flag to check for race condition. You can run go fmt to auto format code in directory. You can install multiple versions of go following this guide

Checking Goroutine Leak

import (
  "go.uber.org/goleak"
)
func TestMain(m *testing.M) {
  goleak.VerifyTestMain(m)
}

Table of Content