2019-04-12 11:58:03 +03:00
|
|
|
|
// Copyright © 2018 Inanc Gumus
|
|
|
|
|
// Learn Go Programming Course
|
|
|
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
|
//
|
2019-10-30 19:34:44 +03:00
|
|
|
|
// For more tutorials : https://learngoprogramming.com
|
|
|
|
|
// In-person training : https://www.linkedin.com/in/inancgumus/
|
|
|
|
|
// Follow me on twitter: https://twitter.com/inancgumus
|
2019-04-12 11:58:03 +03:00
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2019-05-04 20:35:25 +03:00
|
|
|
|
// #2A: Get the key from CLI
|
2019-04-12 11:58:03 +03:00
|
|
|
|
args := os.Args[1:]
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
|
fmt.Println("[english word] -> [turkish word]")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
query := args[0]
|
|
|
|
|
|
2019-05-04 20:35:25 +03:00
|
|
|
|
// #1: Empty Map Literal
|
2019-04-12 11:58:03 +03:00
|
|
|
|
// dict := map[string]string{}
|
|
|
|
|
|
2019-05-04 20:35:25 +03:00
|
|
|
|
// 3: Map Literal
|
2019-04-12 11:58:03 +03:00
|
|
|
|
dict := map[string]string{
|
|
|
|
|
"good": "kötü",
|
|
|
|
|
"great": "harika",
|
|
|
|
|
"perfect": "mükemmel",
|
2019-05-04 20:35:25 +03:00
|
|
|
|
// #4
|
2019-04-12 11:58:03 +03:00
|
|
|
|
// 42: "forty two",
|
|
|
|
|
// "forty two": 42,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2019-05-04 20:35:25 +03:00
|
|
|
|
// #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)
|
2019-04-12 11:58:03 +03:00
|
|
|
|
|
2019-05-04 20:35:25 +03:00
|
|
|
|
// fmt.Printf("Zero Value: %#v\n", dict)
|
|
|
|
|
fmt.Printf("# of Keys : %d\n", len(dict))
|
2019-04-12 11:58:03 +03:00
|
|
|
|
|
2019-05-04 20:35:25 +03:00
|
|
|
|
// #13: compare a map using its printed output
|
2019-04-12 11:58:03 +03:00
|
|
|
|
// 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")
|
|
|
|
|
// }
|
|
|
|
|
|
2019-05-04 20:35:25 +03:00
|
|
|
|
// #12: printing a map (ordered output since Go 1.12)
|
|
|
|
|
// fmt.Printf("%#v\n", dict)
|
2019-04-12 11:58:03 +03:00
|
|
|
|
|
2019-05-04 20:35:25 +03:00
|
|
|
|
// #11
|
|
|
|
|
// for k, v := range dict {
|
|
|
|
|
// fmt.Printf("%q means %#v\n", k, v)
|
|
|
|
|
// }
|
2019-04-12 11:58:03 +03:00
|
|
|
|
|
2019-05-04 20:35:25 +03:00
|
|
|
|
// #9: check for non-existing key: with comma, ok
|
2019-04-12 11:58:03 +03:00
|
|
|
|
// value, ok := dict[query]
|
2019-05-04 20:35:25 +03:00
|
|
|
|
// if !ok {
|
|
|
|
|
// fmt.Printf("%q not found.\n", query)
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// #7: check for non-existing key using zero-value
|
|
|
|
|
// if value == "" {
|
|
|
|
|
// fmt.Printf("%q not found.\n", query)
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// #6: getting values from a map using keys directly
|
|
|
|
|
// fmt.Println("good -> ", dict["good"])
|
|
|
|
|
// fmt.Println("great -> ", dict["great"])
|
|
|
|
|
// fmt.Println("perfect -> ", dict["perfect"])
|
|
|
|
|
|
|
|
|
|
// #2B: retrieve values by key - O(1) efficiency
|
|
|
|
|
// value := dict[query]
|
2019-04-12 11:58:03 +03:00
|
|
|
|
|
|
|
|
|
}
|