Package fmt / Stringer interface / Essential Go

Run Settings
LanguageGo
Language Version
Run Command
package main import ( "fmt" ) type User1 struct { Name string Email string } type User2 struct { Name string Email string } // String satisfies the fmt.Stringer interface // Defining it on type User2 makes it available on *User2 as well func (u User2) String() string { return fmt.Sprintf("%s <%s>", u.Name, u.Email) } func main() { u1 := &User1{ Name: "John Doe", Email: "johndoe@example.com", } fmt.Printf("u1: type: %T, value: %s\n\n", u1, u1) u2 := User2{ Name: "John Doe", Email: "johndoe@example.com", } fmt.Printf("u2: type: %T, value: %s\n\n", u2, u2) // method define on type User2 is also available on type *User2 u3 := &User2{ Name: "John Doe", Email: "johndoe@example.com", } fmt.Printf("u3: type: %T, value: %s\n", u3, u3) }
Editor Settings
Theme
Key bindings
Full width
Lines