person.go 984 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type Person struct {
  7. firstName string
  8. lastName string
  9. }
  10. func upPerson(p *Person) {
  11. p.firstName = strings.ToUpper(p.firstName)
  12. p.lastName = strings.ToUpper(p.lastName)
  13. }
  14. func main() {
  15. // 1- struct as a value type:
  16. var pers1 Person
  17. pers1.firstName = "Chris"
  18. pers1.lastName = "Woodward"
  19. upPerson(&pers1)
  20. fmt.Printf("The name of the person is %s %s\n", pers1.firstName, pers1.lastName)
  21. // 2 - struct as a pointer:
  22. pers2 := new(Person)
  23. pers2.firstName = "Chris"
  24. pers2.lastName = "Woodward"
  25. (*pers2).lastName = "Woodward"
  26. upPerson(pers2)
  27. fmt.Printf("The name of the person is %s %s\n", pers2.firstName, pers2.lastName)
  28. // 3 - struct as a literal:
  29. pers3 := &Person{"Chris", "Woodward"}
  30. upPerson(pers3)
  31. fmt.Printf("The name of the person is %s %s\n", pers3.firstName, pers3.lastName)
  32. }
  33. /* Output:
  34. The name of the person is CHRIS WOODWARD
  35. The name of the person is CHRIS WOODWARD
  36. The name of the person is CHRIS WOODWARD
  37. */