Purpose:
backend application
compile fast, run fast
high level, user friendly
Install:
install go described on their website
install go VSCode extension
follow the instruction when you open a go project in VSCode, install gopls
. It is the official Go language server developed by the Go team to enable you to do auto complete, go to definition, or documentation on hover for a programming language.
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
For more, see Test
Here are some useful links:
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
import (
"go.uber.org/goleak"
)
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
Table of Content