work on some chapters
This commit is contained in:
parent
25dc8ca800
commit
f6ecd99639
@ -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.
|
||||
|
@ -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?
|
||||
|
@ -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)
|
||||
}
|
||||
|
6
src/015-maps/maps.sh
Normal file
6
src/015-maps/maps.sh
Normal file
@ -0,0 +1,6 @@
|
||||
$ go run maps.go
|
||||
map: map[k1:7 k2:13]
|
||||
v1: 7
|
||||
len: 2
|
||||
map: map[k1:7]
|
||||
prs: false
|
@ -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.
|
||||
|
28
src/017-multiple-return-values/multiple-return-values.go
Normal file
28
src/017-multiple-return-values/multiple-return-values.go
Normal file
@ -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)
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
$ go run multiple-return-values.go
|
||||
3
|
||||
7
|
||||
7
|
@ -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)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user