structs_anonymous_fields.go 616 B

12345678910111213141516171819202122232425262728293031323334
  1. package main
  2. import "fmt"
  3. type innerS struct {
  4. in1 int
  5. in2 int
  6. }
  7. type outerS struct {
  8. b int
  9. c float32
  10. int // anonymous field
  11. innerS // anonymous field
  12. }
  13. func main() {
  14. outer := new(outerS)
  15. outer.b = 6
  16. outer.c = 7.5
  17. outer.int = 60
  18. outer.in1 = 5
  19. outer.in2 = 10
  20. fmt.Printf("outer.b is: %d\n", outer.b)
  21. fmt.Printf("outer.c is: %f\n", outer.c)
  22. fmt.Printf("outer.int is: %d\n", outer.int)
  23. fmt.Printf("outer.in1 is: %d\n", outer.in1)
  24. fmt.Printf("outer.in2 is: %d\n", outer.in2)
  25. // with a struct-literal:
  26. outer2 := outerS{6, 7.5, 60, innerS{5, 10}}
  27. fmt.Println("outer2 is: ", outer2)
  28. }