Properties of GO

  1. Statically typed language - declare types explicitly or must be inferred, and types cannot change without type conversion
  2. Strongly typed language - operation allowed to be performed on a variable depends on the types
  3. Compiled language - converts into machine code instead of interpreted as it is running
  4. Fast compile time
  5. Built in concurrency - GoRoutines
  6. Simplicity - concise syntax and garbage collection

Structure of Go code

  • Package: folder that contains a collection of Go files
  • Module: collection of packages
    • It is what is created when we initialise a module

Initialising a Go Project

go mod init [project name]

  • usually instead of project name, it will be the location of the GitHub repository

Syntax

  • Specify the package the Go file it belongs to with package [name]
    • Name used must be the same for all files within the folder
  • All variables and packages declared must be used, else the compiler throws an error
  • package main is a special package name that tells the compiler to find the entry point function (main) here
package main
import "fmt"
 
func main() {
	fmt.Println("Hello World")
}

Compiling and running a program

go build main.go
./main

which is equivalent to go run main.go