basic types:
bool
; // false
is zero (initial) value for bool
string
; // “” (the empty string) is zero (initial) value for string
int, int8, int16, int32, int64
uint, uint8, uint16, uint32, uint64, uintptr
byte
// alias for uint8
rune
// alias for int32, Unicode point
float32, float64
complex64, complex128
package main
import (
"fmt"
"math/cmplx"
)
var (
ToBe bool = false
MaxInt uint64 = 1<<64 - 1
z complex128 = cmplx.Sqrt(-5 + 12i)
)
Type conversion: explicit only; new_type(variable_name)
.
Type inference: by :=
or var
when the right hand side of the declaration is typed, the new variable is of that the same type:
{
var i int
j := i // j is an int
}
when 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.
package main
import "fmt"
func main() {
v := 42 // int
v2 := 42.01 // float64
v3 := 9.4 + 0.3i // complex128
fmt.Printf("v is of type %T\n", v)
fmt.Printf("v2 is of type %T\n", v2)
fmt.Printf("v3 is of type %T\n", v3)
}
Functions/Variables: type comes after the variable name; omit redundant names; multiple returns; named return values;
func add(x int, y int) int {
return x + y
}
func swap(x, y int) (int, int) { //omit the type but the last, for consecutive shared type name.
return y, x // Go can return any number of result
}
Named return values:
func split(sum int) (x, y int) {
x = sum * 4 / 9 // named return values are treated variables defined at the top of a function
y = sum - x
return // a 'naked' return returns named return values
}
func main() {
fmt.Println(split(17)) // will print '7 10'
}
Motivation: the confusing syntxt in C from Go’s delcaration Syntax:
{
char *argv[]; // name in middle
int (*fp) (int (*) (int, int), int); // function passed as parameter
int (*(*fp)(int (*)(int, int), int))(int, int); // return a function
}
C’s syntax is read in Spiral: Clockwise/Spiral Rule.
Go’s syntax, read from left to right:
{
x int // x: int
p *int // p: pointer to int
a [3]int // a: array [3] of int
f func(func(int,int) int, int) int //function passed as a parameter
f func(func(int,int) int, int) func(int,int) int //returns a function
}
Variables: var
and :=
var c, python bool
var x, y int = 1, 2 // initialization
var cxx, python3, java = true, false, "no!" // type can be omitted if initialization is present
func main() {
var i int
prolog, coq := true, "Yes!" // short variable declaration by ':=', only used insie the function
fmt.Println(i, c, python)
fmt.Println(x,y,cxx,python3,java)
fmt.Println(prolog, coq)
}
Like variables but with const
and its expression for value, e.g. const Pi = 3.14
constant cannot use :=
Numeric constants are high-precision values.
Packages: package main
Imports: import "fmt"
Exported names: begins with a capital letter;
package main
import (
"fmt"
"math/rand"
)
import "math"
func main(){
fmt.Println("My favorite number is", rand.Intn(1000))
fmt.Println("PI:", math.Pi)
}
If you could revise
the fundmental principles of
computer system design
to improve security...
... what would you change?