starting go by example

This commit is contained in:
2021-12-31 15:36:34 +01:00
parent bafba8a7b6
commit afe165089d
11 changed files with 340 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
package main
import "fmt"
// Maps are Gos built-in associative data type
func main() {
// create an empty map, use the builtin: 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 will show all of its key/value pairs
fmt.Println("map:", m)
// get a value for a key with name[key]
v1 := m["k1"]
fmt.Println("v1: ", v1)
// len returns the number of key/value pairs
fmt.Println("len:", len(m))
// delete removes key/value pairs
delete(m, "k2")
fmt.Println("map:", m)
// optional second return value when getting a value from a map indicates if the key was present in the map
_, prs := m["k2"]
fmt.Println("prs:", prs)
// declare and initialize a new map in the same line
n := map[string]int{"foo": 1, "bar": 2}
fmt.Println("map:", n)
}