💻 Tech
In Go, you can implement fmt package’s Stringer
interface to customize how you want your struct to be printed. In the example below, Guo
has its own way of printing itself while Pays
relies on the default way it will be printed
type Guo struct {
name string
}
type Pays struct {
name string
}
func (g Guo) String() string {
return fmt.Sprintf("Wo de guo shi %v", g.name)
}
func main() {
fmt.Println(Guo{name: "zhong guo"}) // Wo de guo shi zhong guo
fmt.Println(Pays{name: "france"}) // {france}
}