Files
learngo/22-maps/03-internals-cloning/main.go

62 lines
1.2 KiB
Go
Raw Normal View History

2019-04-12 11:58:03 +03:00
// 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",
2019-05-04 20:35:25 +03:00
"awesome": "mükemmel", // #5
2019-04-12 11:58:03 +03:00
}
2019-05-04 20:35:25 +03:00
// turkish := dict // #1
2019-04-12 11:58:03 +03:00
// turkish["good"] = "güzel"
// dict["great"] = "kusursuz"
2019-05-04 20:35:25 +03:00
delete(dict, "awesome") // #6
delete(dict, "awesome") // #7: no-op
2019-04-12 11:58:03 +03:00
delete(dict, "notexisting")
2019-05-04 20:35:25 +03:00
// dict = nil // #8
for k := range dict { // #9
2019-04-12 11:58:03 +03:00
delete(dict, k)
}
2019-05-04 20:35:25 +03:00
// turkish := make(map[string]string) // #2
turkish := make(map[string]string, len(dict)) // #3
2019-04-12 11:58:03 +03:00
for k, v := range dict {
turkish[v] = k
}
2019-05-04 20:35:25 +03:00
// fmt.Printf("english: %q\nturkish: %q\n", dict, turkish) // #4
2019-04-12 11:58:03 +03:00
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)
}