method1.go 495 B

1234567891011121314151617181920212223242526272829
  1. package main
  2. import "fmt"
  3. type TwoInts struct {
  4. a int
  5. b int
  6. }
  7. func main() {
  8. two1 := new(TwoInts)
  9. two1.a = 12
  10. two1.b = 10
  11. fmt.Printf("The sum is: %d\n", two1.AddThem())
  12. fmt.Printf("Add them to the param: %d\n", two1.AddToParam(20))
  13. // literal:
  14. two2 := TwoInts{3, 4}
  15. fmt.Printf("The sum is: %d\n", two2.AddThem())
  16. }
  17. func (tn *TwoInts) AddThem() int {
  18. return tn.a + tn.b
  19. }
  20. func (tn *TwoInts) AddToParam(param int) int {
  21. return tn.a + tn.b + param
  22. }