embed_func1.go 745 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type Log struct {
  6. msg string
  7. }
  8. type Customer struct {
  9. Name string
  10. log *Log
  11. }
  12. func main() {
  13. // c := new(Customer)
  14. // c.Name = "Barak Obama"
  15. // c.log = new(Log)
  16. // c.log.msg = "1 - Yes we can!"
  17. // shorter:
  18. c := &Customer{"Barak Obama", &Log{"1 - Yes we can!"}}
  19. // fmt.Println(c) // &{Barak Obama 1 - Yes we can!}
  20. c.Log().Add("2 - After me the world will be a better place!")
  21. //fmt.Println(c.log)
  22. fmt.Println(c.Log())
  23. }
  24. func (l *Log) Add(s string) {
  25. l.msg += "\n" + s
  26. }
  27. func (l *Log) String() string {
  28. return l.msg
  29. }
  30. func (c *Customer) Log() *Log {
  31. return c.log
  32. }
  33. /* Output:
  34. 1 - Yes we can!
  35. 2 - After me the world will be a better place!
  36. */