add: maps

This commit is contained in:
Inanc Gumus
2019-04-12 11:58:03 +03:00
parent 7247967ef3
commit e591eb0d45
6 changed files with 319 additions and 0 deletions

View File

@ -0,0 +1,35 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"os"
)
func main() {
args := os.Args[1:]
if len(args) != 1 {
fmt.Println("[english word] -> [turkish word]")
return
}
query := args[0]
english := []string{"good", "great", "perfect"}
turkish := []string{"iyi", "harika", "mükemmel"}
// O(n) -> Inefficient: Depends on 'n'.
for i, w := range english {
if query == w {
fmt.Printf("%q means %q\n", w, turkish[i])
return
}
}
fmt.Printf("%q not found\n", query)
}

View File

@ -0,0 +1,52 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
)
func main() {
/*
#1: Nil Map: Read-Only
*/
var dict map[string]string
fmt.Printf("Zero-value of a map: %#v\n", dict)
/*
#5: You cannot assign to a nil map.
*/
// dict["up"] = "yukarı"
// dict["down"] = "aşağı"
/*
#2: Map retrieval is O(1) — on average.
*/
key := "good"
//
// #4: you can use an uninitialized map without checking it is nil
//
// if dict != nil {
value := dict[key]
fmt.Printf("%q means %#v\n", key, value)
// }
/*
#3: Cannot use non-comparable types as map key types
*/
// var broken map[[]int]int
// var broken map[map[int]string]bool
//
// A map can only be compared to nil value
// _ = dict == nil
/*
#1 Step 2
*/
fmt.Printf("The dictionary contains %d words.\n", len(dict))
}

View File

@ -0,0 +1,119 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"os"
)
func main() {
/*
#2—A: Get the key from CLI
*/
args := os.Args[1:]
if len(args) != 1 {
fmt.Println("[english word] -> [turkish word]")
return
}
query := args[0]
/*
#1: Empty Map Literal
*/
// dict := map[string]string{}
/*
#3: Map Literal
Creates, initializes and returns a new map
with the given key-value pairs
*/
dict := map[string]string{
"good": "kötü",
"great": "harika",
"perfect": "mükemmel",
/*
#4: keys and values should be the same type
(key and value types do not need to be the same)
*/
// 42: "forty two",
// "forty two": 42,
}
// you can only add new pairs to an initialized map
dict["up"] = "yukarı" // adds a new pair
dict["down"] = "aşağı" // adds a new pair
dict["good"] = "iyi" // #5: overwrites the value at the key: "good"
dict["mistake"] = "" // #8: a key with a zero-value
// you can query an uninitialized map for its length
fmt.Printf("The dictionary contains %d words.\n", len(dict))
/*
#6: getting values from a map using keys directly
*/
// fmt.Println("good -> ", dict["good"])
// fmt.Println("great -> ", dict["great"])
// fmt.Println("perfect -> ", dict["perfect"])
/*
#11: looping over a map might be a sign of a design mistake
in your software.
(it prints unordered output)
*/
// for k, v := range dict {
// fmt.Printf("%q means %#v\n", k, v)
// }
/*
#12: printing a map (ordered output since Go 1.12)
*/
// fmt.Printf("%#v\n", dict)
/*
#13: compare a map using its printed output
(handy when testing)
*/
// copied := map[string]string{"up": "yukarı", "down": "aşağı",
// "mistake": "", "good": "iyi", "great": "harika",
// "perfect": "mükemmel"}
// first := fmt.Sprintf("%s", dict)
// second := fmt.Sprintf("%s", copied)
// if first == second {
// fmt.Println("Maps are equal")
// }
/*
#2—B: retrieve values by key - O(1) efficiency
(even from an uninitialized map)
*/
// value := dict[query]
/*
#7: check for non-existing key using zero-value
*/
// if value == "" {}
/*
#9: check for non-existing key: with comma, ok
*/
// value, ok := dict[query]
// if !ok {}
/*
#10: comma ok in a short if
*/
if value, ok := dict[query]; ok {
fmt.Printf("%q means %#v\n", query, value)
return
}
fmt.Printf("%q not found.\n", query)
}

View File

@ -0,0 +1,77 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"os"
)
func main() {
args := os.Args[1:]
if len(args) != 1 {
fmt.Println("[english word] -> [turkish word]")
return
}
query := args[0]
dict := map[string]string{
"good": "iyi",
"great": "harika",
"perfect": "mükemmel",
// #5: this overwrites the mükemmel in the turkish map
"awesome": "mükemmel",
}
// #1: map value is a pointer to a real map in the memory
// turkish := dict
// turkish["good"] = "güzel"
// dict["great"] = "kusursuz"
// #6: delete can delete a pair from a map
delete(dict, "awesome")
// #7: no-op
delete(dict, "awesome")
delete(dict, "notexisting")
// #8: destroying a map completely (false)
// dict = nil
// #9: destroying a map completely (true)
// replaces the whole loop with a single instruction:
// "call runtime.mapclear()"
for k := range dict {
delete(dict, k)
}
// #2: make initializes a new map value and returns a pointer to it
// turkish := make(map[string]string)
// #3: make(T, hint) => hint is an advice for initing a large enough map
turkish := make(map[string]string, len(dict))
for k, v := range dict {
turkish[v] = k
}
// #4: add turkish dictionary
// fmt.Printf("english: %q\nturkish: %q\n", dict, turkish)
if value, ok := dict[query]; ok {
fmt.Printf("%q means %#v\n", query, value)
return
}
if value, ok := turkish[query]; ok {
fmt.Printf("%q means %#v\n", query, value)
return
}
fmt.Printf("%q not found.\n", query)
}

31
22-maps/05-sets/main.go Normal file
View File

@ -0,0 +1,31 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import "fmt"
func main() {
// let's create a slice of coders.
coders := []string{"pike", "thompson", "lovelace", "knuth"}
// let's create a map for tracking whether they are gophers or not.
gophers := make(map[string]bool, len(coders))
// let's mark the gophers
gophers["thompson"] = true
gophers["pike"] = true
fmt.Println(gophers["pike"])
fmt.Println(gophers["lovelace"])
if _, ok := gophers["pike"]; ok {
fmt.Println("pike is a gopher")
} else {
fmt.Println("pike is NOT a gopher")
}
}

View File

@ -0,0 +1,5 @@
# IDEAS
* Retrieve sorted map keys
* Roman literals