Source file tour/methods/stringer.go

     1  //go:build OMIT
     2  
     3  package main
     4  
     5  import "fmt"
     6  
     7  type Person struct {
     8  	Name string
     9  	Age  int
    10  }
    11  
    12  func (p Person) String() string {
    13  	return fmt.Sprintf("%v (%v years)", p.Name, p.Age)
    14  }
    15  
    16  func main() {
    17  	a := Person{"Arthur Dent", 42}
    18  	z := Person{"Zaphod Beeblebrox", 9001}
    19  	fmt.Println(a, z)
    20  }
    21  

View as plain text