personex1.go 952 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. upPerson(*pers2)
  26. fmt.Printf("The name of the person is %s %s\n", pers2.firstName, pers2.lastName)
  27. // 3 - struct as a literal:
  28. pers3 := &Person{"Chris", "Woodward"}
  29. upPerson(*pers3)
  30. fmt.Printf("The name of the person is %s %s\n", pers3.firstName, pers3.lastName)
  31. }
  32. /* Output:
  33. The name of the person is Chris Woodward
  34. The name of the person is Chris Woodward
  35. The name of the person is Chris Woodward
  36. */