diff --git a/src/010-if-else/if-else.go b/src/010-if-else/if-else.go index 2bce8a6..604b044 100644 --- a/src/010-if-else/if-else.go +++ b/src/010-if-else/if-else.go @@ -1,5 +1,6 @@ // ## If/Else +// If/else in go is straight-forward package main import "fmt" @@ -11,7 +12,12 @@ func main() { fmt.Print("7 is ") if 7%2 == 0 { fmt.Println("even") - } else { + } else if 7%2 == 1 { fmt.Println("odd") + } else { + fmt.Println("???") } } + +// There is no ternary operator (i.e. `?`) in Go, so you'll +// need to use a full if/else even for very basic conditions. diff --git a/src/011-switch/switch.go b/src/011-switch/switch.go index 42c11a4..57105d7 100644 --- a/src/011-switch/switch.go +++ b/src/011-switch/switch.go @@ -1,5 +1,7 @@ // ## Switch +// Switch statements allow... + package main import "fmt" @@ -7,6 +9,7 @@ import "fmt" func main() { fmt.Print("Write 3 as ") i := 3 + // Some basic commentary on switches. switch i { case 0: fmt.Println("zero") @@ -20,7 +23,10 @@ func main() { fmt.Println("four") case 5: fmt.Println("five") + // The `default` branch is optional in a `switch`. default: fmt.Println("???") } } + +// todo: more complex / non-constant switch? diff --git a/src/015-maps/maps.go b/src/015-maps/maps.go index cd898a2..6ff889e 100644 --- a/src/015-maps/maps.go +++ b/src/015-maps/maps.go @@ -1,20 +1,45 @@ // ## Maps +// Maps are Go's built-in associative data type (sometimes +// called "hashes" or "dicts" in other languages). + package main import "fmt" func main() { - x := make(map[string]int) - x["this"] = 7 - x["that"] = 13 - fmt.Println(x) - fmt.Println(x["this"]) - fmt.Println(x["missing"]) - fmt.Println(len(x)) - delete(x, "that") - fmt.Println(x["that"]) - fmt.Println(len(x)) - _, present := x["that"] - fmt.Println(present) + // To create an empty map, use the builtin `make`: + // `make(map[key-type]val-type)`. + m := make(map[string]int) + + // Set key/value pairs using typical `name[key] = val` + // syntax. + m["k1"] = 7 + m["k2"] = 13 + + // Printing a map with e.g. `Println` will show all of + // its key/value pairs, which can be useful for + // debugging. + fmt.Println("map:", m) + + // Get a value for a key with `name[key]`. + v1 := m["k1"] + fmt.Println("v1: ", v1) + + // The builtin `len` function returns the number of + // key/value pairs when called on a map. + fmt.Println("len:", len(m)) + + // The builtin `delete` removes key/value pairs from + // a map + delete(m, "k2") + fmt.Println("map:", m) + + // The optional second return value when getting a + // value from a map indiciates if the key was present + // in the map. This can be used to disambiguate + // between missing keys and keys with zero values + // like `0` or `""`. + _, prs := m["k2"] + fmt.Println("prs:", prs) } diff --git a/src/015-maps/maps.sh b/src/015-maps/maps.sh new file mode 100644 index 0000000..c081d1a --- /dev/null +++ b/src/015-maps/maps.sh @@ -0,0 +1,6 @@ +$ go run maps.go +map: map[k1:7 k2:13] +v1: 7 +len: 2 +map: map[k1:7] +prs: false diff --git a/src/016-functions/functions.go b/src/016-functions/functions.go index 4c793b1..e97d891 100644 --- a/src/016-functions/functions.go +++ b/src/016-functions/functions.go @@ -1,20 +1,35 @@ // ## Functions +// Funcations are critical in Go as in any other language. +// We'll look at some basic examples first. + package main import "fmt" +// The syntax is `func name(args) returns { body }`. Note +// the the types of arguments come after their name. +// In this case we're taking a slice of floats and returning +// a single float value. func avg(vals []float64) float64 { total := 0.0 for _, val := range vals { total += val } + // Go requires an expliciti return, i.e. it won't + // automatically return the value of the last expression. return total / float64(len(vals)) } func main() { input := []float64{98, 93, 77, 82, 83} fmt.Println(input) + // Call a function just as you'd expect, with + // `name(args)`. output := avg(input) fmt.Println(output) } + +// There are several other features to Go functions, including +// multiple return values and varargs, which we'll look at +// next. diff --git a/src/017-multiple-return-values/multiple-return-values.go b/src/017-multiple-return-values/multiple-return-values.go new file mode 100644 index 0000000..883decc --- /dev/null +++ b/src/017-multiple-return-values/multiple-return-values.go @@ -0,0 +1,28 @@ +// ## Multiple Return Values + +// Go has built-in support for multiple return values. +// This feature is used often in idiomatic Go, for example +// to return both result and error values from a function. + +package main + +import "fmt" + +// The `(int, int)` in this signature shows that the +// function returns 2 ints. +func vals() (int, int) { + return 3, 7 +} + +func main() { + // Here we use the 2 different return values from the + // call, i.e. perform multiple assignement. + a, b := vals() + fmt.Println(a) + fmt.Println(b) + + // In cases were you only want a subset of the + // returned values, use the blank identifier `_`. + _, c := vals() + fmt.Println(c) +} diff --git a/src/017-multiple-return-values/returns.sh b/src/017-multiple-return-values/multiple-return-values.sh similarity index 95% rename from src/017-multiple-return-values/returns.sh rename to src/017-multiple-return-values/multiple-return-values.sh index f9bd631..e29a786 100644 --- a/src/017-multiple-return-values/returns.sh +++ b/src/017-multiple-return-values/multiple-return-values.sh @@ -1,3 +1,4 @@ $ go run multiple-return-values.go 3 7 +7 diff --git a/src/017-multiple-return-values/multiple-returns-values.go b/src/017-multiple-return-values/multiple-returns-values.go deleted file mode 100644 index b01f2d6..0000000 --- a/src/017-multiple-return-values/multiple-returns-values.go +++ /dev/null @@ -1,24 +0,0 @@ -// ## Multiple Return Values - -// In Go, a function can return multiple values. -// This feature is used often, for example to -// return a result and an error from a function. - -package main - -import "fmt" - -// The `(int, int)` in this signature shows that the -// function returns 2 ints. -func vals() (int, int) { - return 3, 7 - -} - -func main() { - // Use the 2 different return values from the call, - // i.e. multiple assignement. - x, y := vals() - fmt.Println(x) - fmt.Println(y) -}