├── LICENSE ├── README.md ├── chapter01.markdown ├── chapter02.markdown ├── chapter03.markdown └── chapter04.markdown /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | 3 | Copyright (C) 2015-2017 Stefan Schröder 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Golang-NestedDatastructures-Tutorial 2 | ==================================== 3 | 4 | A hands-on tutorial for nested datastructures in Go. 5 | 6 | 7 | Chapter 1: Arrays of Arrays 8 | 9 | Chapter 2: Maps of Arrays 10 | 11 | Chapter 3: Arrays of Maps 12 | 13 | Chapter 4: Maps of Maps 14 | -------------------------------------------------------------------------------- /chapter01.markdown: -------------------------------------------------------------------------------- 1 | # Arrays of Arrays # 2 | 3 | In the first chapter we want to create arrays of arrays. 4 | Because slices and arrays are handled very similarly, slices are handled here as well. 5 | 6 | Please note that a complete source code file is available at the same location where you found this markdown page. 7 | 8 | ## Create ## 9 | 10 | Create nested Array Of Array 11 | 12 | aoa := [3][2]string{ [...]string{"a","b"}, [...]string{"c","d"}, [...]string{"e", "f"}, } 13 | 14 | Create nested Array Of Slices 15 | 16 | aos := [3][]string{ []string{"a","b"}, []string{"c","d"}, []string{"e", "f"}, } 17 | 18 | ## Update ## 19 | 20 | Set a single entry from nested array 21 | 22 | aoa[1][1] = "OverwrittenInArray" 23 | aos[1][1] = "OverwrittenInSlice" 24 | 25 | Put entire array 26 | 27 | aoa[2] = [...]string{"OverwritingInArray", "AnotherInArray"} 28 | 29 | Put entire slice 30 | 31 | aos[2] = []string{"OverwritingInSlice", "AnotherInSlice"} 32 | 33 | ## Retrieve ## 34 | 35 | Get one array 36 | 37 | fmt.Printf("The second array is %v.\n", aoa[1]) 38 | 39 | Prints "The second array is [c OverwrittenInArray]." 40 | 41 | Get one slice 42 | 43 | fmt.Printf("The second slice is %v.\n", aos[1]) 44 | 45 | Prints: "The second slice is [c OverwrittenInSlice]." 46 | 47 | Get a single entry from nested array 48 | 49 | fmt.Printf("Entry 2,1 in array is %s.\n", aoa[2][1]) // "f" 50 | 51 | Get a single entry from nested slice 52 | 53 | fmt.Printf("Entry 2,1 in slice is %s.\n", aos[2][1]) // "f" 54 | 55 | ## Print entire Array of Arrays by element ## 56 | 57 | for i:=0; i %d\n", i, j, arr[i][j]) 31 | } 32 | fmt.Printf("\n") 33 | } 34 | 35 | Second example. All in one. 36 | 37 | aomB1 := [2]map[string]int{ map[string]int{"one": 1}, map[string]int{"two" : 2,}, } 38 | aomB3 := [...]map[string]int{ map[string]int{"one": 1}, map[string]int{"two" : 2,}, } 39 | aomB2 := []map[string]int{ map[string]int{"one": 1}, map[string]int{"two" : 2,}, } 40 | fmt.Printf("Entire DSC: %v\n", aomB1) 41 | fmt.Printf("Entire DSC: %v\n", aomB2) 42 | fmt.Printf("Entire DSC: %v\n", aomB3) 43 | -------------------------------------------------------------------------------- /chapter04.markdown: -------------------------------------------------------------------------------- 1 | # Map of Maps # 2 | 3 | Create a map that maps keys of type string to maps. 4 | The nested maps again map strings to strings. 5 | 6 | mom := map[string]map[string]string{ 7 | "jp": map[string]string{"one": "ichi", "two": "ni"}, 8 | "de": map[string]string{"one": "eins", "three": "drei"}, } 9 | 10 | Add a map 11 | 12 | mom["fr"] = map[string]string{"one" : "un", "four" : "quatre"} 13 | 14 | Add an entry to one of the nested maps. 15 | 16 | mom["jp"]["eight"] = "hachi" 17 | 18 | Delete an entry in one of the nested maps. 19 | 20 | delete(mom["jp"], "one") 21 | 22 | Delete one of the nested maps 23 | 24 | delete(mom, "de") 25 | 26 | Print all elements 27 | 28 | for i := range(mom) { 29 | fmt.Printf("(%s) %v\n", i, mom[i]) 30 | for j := range(mom[i]) { 31 | fmt.Printf(" (%s) %s => %s\n", i, j, mom[i][j]) 32 | } 33 | } 34 | 35 | Print entire data structure 36 | 37 | fmt.Printf("Entire DSC: %v\n", mom) 38 | 39 | --------------------------------------------------------------------------------