mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-11 23:08:34 +08:00
add 08.0.md 08.1.md
This commit is contained in:
21
eBook/examples/chapter_8/make_maps.go
Normal file
21
eBook/examples/chapter_8/make_maps.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var mapLit map[string]int
|
||||
//var mapCreated map[string]float32
|
||||
var mapAssigned map[string]int
|
||||
|
||||
mapLit = map[string]int{"one": 1, "two": 2}
|
||||
mapCreated := make(map[string]float32)
|
||||
mapAssigned = mapLit
|
||||
|
||||
mapCreated["key1"] = 4.5
|
||||
mapCreated["key2"] = 3.14159
|
||||
mapAssigned["two"] = 3
|
||||
|
||||
fmt.Printf("Map literal at \"one\" is: %d\n", mapLit["one"])
|
||||
fmt.Printf("Map created at \"key2\" is: %f\n", mapCreated["key2"])
|
||||
fmt.Printf("Map assigned at \"two\" is: %d\n", mapLit["two"])
|
||||
fmt.Printf("Map literal at \"ten\" is: %d\n", mapLit["ten"])
|
||||
}
|
11
eBook/examples/chapter_8/map_func.go
Normal file
11
eBook/examples/chapter_8/map_func.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package main
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
mf := map[int]func() int{
|
||||
1: func() int { return 10 },
|
||||
2: func() int { return 20 },
|
||||
5: func() int { return 50 },
|
||||
}
|
||||
fmt.Println(mf)
|
||||
}
|
Reference in New Issue
Block a user