var keyword or the shorthand := (only inside of functions / methods to simplify parsing!).var a int
// or
a := 2 // in functions or methods
%d %[1]v will reuse the first passed in argument (e.g. if we want to print a single variable twice in a Printf, you’d normally do fmt.Printf("%d %d", a, a), but with this you just need to do fmt.Printf("%d %[1]v", a) and that parameter a will be reused)const(
a = 1
b = 3 * 100
s = "hello"
)
byte is a synonym for uint8rune is a synonym for int32 for charactersstring is an immutable sequence of “characters”
runes\n)`string with "quotes"`
string is the number of UTF-8 bytes required to encode it, NOT THE NUMBER OF LOGICAL CHARACTERSstrings package contains useful string functions==, slices are obviously not. Arrays can be used as map keys, slices cannotcopy and append helper operatorsint you’d get 0)var m map[string]int // nil map (reading any key will return the default value of the map value type)
_m := make(map[string]int) // empty non-nil map
make creates the underlying hash table and allocates memory etc. It is required to instantiate and write to a mapvar m = map[string]int {
"hello": 1
}
p := map[string]int{} // Empty non nil map
a, ok := p["hello"] // Returns 0, false since the key "hello" doesn't exist
p["hello"]++
b, ok := p["hello"] // Returns 1, true
if w, ok := p["the"]; ok {
// Useful if we want to do something if an entry is / isn't in the map
}
&myMap["Hello"])
nil (From https://www.youtube.com/watch?v=ynoY2xz-F8s)nil indicates the absence of something, with part of the Go philosophy being to make the zero value usefulnil value has no type; it is defined for the following constructs:
var s fmt.Stringer // This is a nil interface with no concrete type and no value (nil, nil)
fmt.Println(s == nil) // Will print true since (nil, nil) == nil
//---
var p *Person // This Person satisfies the person interface
var s fmt.Stringer = p // Now we have (*Person, nil) - a concrete type (*Person) but still no value. This is now no longer equal to nil
//---
func do() error { // This will return the nil pointer wrapped in the error interface (*doError, nil)
var err *doError
return err // This is a nil pointer of type *doError
}
fmt.Println(do() == nil) // Will be FALSE because of the above example - (*doError, nil) != nil!!!
// It is good practice to not define or return concrete error variables
if x, err := doSomething(); err != nil {
return err
}
for i := range someArr {
// i is an index here. Remember this - this mistake can happen often. i is the INDEX NOT THE VALUE.
// If you want to range over the values you can use the blank identifier like for _, v := range someArray
}
for i, v := range someArr {
// i is an index, v is the value at that index
// The value v is COPIED - don't modify. If the values are some large struct, it might be better to use the explicit indexing for loop
}
for k := range someMap {
// Looping over all keys in a map
}
for k, v := range someMap {
// Getting the keys and values in the loop
}
for {
// Infinite loop
}
switch someVal {
case 0,1,2:
fmt.Println("Low")
case 3,4,5:
// Noop
default:
fmt.Println("Other")
}
a := 3
switch {
case a <= 2:
case a == 8:
default:
// Do something
}
main package:=
init() function for a package, however using this isn’t really recommendedvar keywordvar a int
var a int = 1
var c = 1 // Type inference
var d = 1.0
// Declaration block for simplicity
var (
x, y int
z float64
s string
)
:=:= is used to declare and assign to a variableerr := doSomething()
err := doSomethingElse() // This is wrong, you can't re-declare err
x, err := doSomethingOther() // This is fine since you are declaring the new var x, and just reassigning err from the original assignment on the skip line above
if _,err := do(); err != nil), that err declaration is local to the control structure scope (that if block scope).func do() error {
var err error
for {
n, err := f.Read(buf)
if err != nil {
break
}
doSomething(buf)
}
return err
}
type keyword
type x int means that you can’t assign something with type x to int or vice versa, you would have to use a type conversion like var thing x = x(12)