mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 01:55:35 +08:00
update book code
This commit is contained in:
27
eBook/examples/chapter_10/new_make.go
Normal file
27
eBook/examples/chapter_10/new_make.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// annoy1.go
|
||||
package main
|
||||
|
||||
type Foo map[string]string
|
||||
type Bar struct {
|
||||
thingOne string
|
||||
thingTwo int
|
||||
}
|
||||
|
||||
func main() {
|
||||
// OK:
|
||||
y := new(Bar)
|
||||
(*y).thingOne = "hello"
|
||||
(*y).thingTwo = 1
|
||||
// not OK:
|
||||
z := make(Bar) // compile error: cannot make type Bar
|
||||
z.thingOne = "hello"
|
||||
z.thingTwo = 1
|
||||
// OK:
|
||||
x := make(Foo)
|
||||
x["x"] = "goodbye"
|
||||
x["y"] = "world"
|
||||
// not OK:
|
||||
u := new(Foo)
|
||||
(*u)["x"] = "goodbye" // !! panic !!: runtime error: assignment to entry in nil map
|
||||
(*u)["y"] = "world"
|
||||
}
|
Reference in New Issue
Block a user