new_make.go 489 B

123456789101112131415161718192021222324252627
  1. // annoy1.go
  2. package main
  3. type Foo map[string]string
  4. type Bar struct {
  5. thingOne string
  6. thingTwo int
  7. }
  8. func main() {
  9. // OK:
  10. y := new(Bar)
  11. (*y).thingOne = "hello"
  12. (*y).thingTwo = 1
  13. // not OK:
  14. z := make(Bar) // compile error: cannot make type Bar
  15. z.thingOne = "hello"
  16. z.thingTwo = 1
  17. // OK:
  18. x := make(Foo)
  19. x["x"] = "goodbye"
  20. x["y"] = "world"
  21. // not OK:
  22. u := new(Foo)
  23. (*u)["x"] = "goodbye" // !! panic !!: runtime error: assignment to entry in nil map
  24. (*u)["y"] = "world"
  25. }