[Go] Basic

Go Commands

go fmt: format the .go files in the current directory
go fmt ./...: format everything in the current and children directory.
go run <FILE_NAME>: build and run code
go build <FILE_NAME>: compile and build a binary file, can be executed by $ ./<FILE_NAME>
go env: show all environment variables for go
go install <FILE_NAME>:

- For an executable:
    - compiles the program
    - names the executable the folder name holding the code
    - puts the executable in **workspace/bin** `$GOPATH/bin`
- For a package
    - compiles the package
    - puts the executable in **workspace/pkg** `$GOPATH/pkg`

Go Workspace

GOPATH: Points to your workspace
GOROOT: Points to your binary installation of Go

Packages

Every Go program is made up of packages.

In Go, a name is exported if it begins with a capital letter. For example, Pizza is an exported name, as is Pi, which is exported from the math package.

1
2
3
func main() {
fmt.Println(math.Pi)
}

Functions

Type comes after the variable name.

Go’s return values may be named. If so, they are treated as variables defined at the top of the function.

A return statement without arguments returns the named return values. This is known as a “naked” return.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
func add(x int, y int) int {
return x + y
}

func add(x, y int) int {
return x + y
}

// return multiple results
func swap(x, y string) string {
return y, x
}

func main() {
a, b := swap("hello", "world")
fmt.Println(a, b)
}

// named return value
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return
}

Variables

The var statement declares a list of variables; as in function argument lists, the type is last.

A var statement can be at package or function level. We see both in this example.

1
2
3
4
5
6
var c, python, java bool

func main() {
var i int
fmt.Println(i, c, python, java)
}

Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type.

Outside a function, every statement begins with a keyword (var, func, and so on) and so the := construct is not available.

1
2
3
4
5
6
func main() {
var i, j int = 1, 2
k := 3
c, python, java := true, false, "no"
fmt.Println(i, j, k, c, python, java)
}

Basic Types

1
2
3
4
5
6
7
8
9
10
11
12
bool
string
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8
rune // alias for int32
// represents a unicode point

float32 float64

complex64 complex128

The int, uint, and uintptr types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems. When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import (
"fmt"
"math/cmplx"
)

var (
ToBe bool = false
MaxInt uint64 = 1<<64 - 1
z complex64 = cmplx.Sqrt(-5 + 12i)
)

func main() {
fmt.Println("Type: %T Value: %v\n", ToBe, ToBe)
fmt.Println("Type: %T Value: %v\n", MaxInt, MaxInt)
fmt.Println("Type: %T Value: %v\n", z, z)
}

Type Conversion

The expression T(v) converts the value v to type T.

1
2
3
4
5
6
var i int = 42
var f float64 = float64(i)
var u uint = uint(f)

i := 42
f := float64(i)

When declaring a variable without specifying an explicit type (either by using the := syntax or var = expression syntax), the variable’s type is inferred from the value on the right hand side.

When the right hand side of the declaration is typed, the new variable is of that same type:

1
2
var i int
j := i // j is also int

But when the right hand side contains an untyped numeric constant, the new variable may be an int, float64, or complex128 depending on the precision of the constant:

1
2
3
i := 42 // int
f := 3.124 // float64
g := 0.8 + 0.5i // complex128

Constants

Constants are declared like variables, but with the const keyword.

Constants can be character, string, boolean, or numeric values.

Constants cannot be declared using the := syntax.

1
2
3
4
func main() {
const World = "world"
fmt.Println("Hello", World)
}

Numeric constants are high-precision values.

An untyped constant takes the type needed by its context.