pointer_value.go 345 B

1234567891011121314151617181920212223242526272829
  1. // pointer_value.go
  2. package main
  3. import (
  4. "fmt"
  5. )
  6. type B struct {
  7. thing int
  8. }
  9. func (b *B) change() { b.thing = 1 }
  10. func (b B) write() string { return fmt.Sprint(b) }
  11. func main() {
  12. var b1 B // b1 is value
  13. b1.change()
  14. fmt.Println(b1.write())
  15. b2 := new(B) // b2 is pointer
  16. b2.change()
  17. fmt.Println(b2.write())
  18. }
  19. /* Output:
  20. {1}
  21. {1}
  22. */