Last active
September 10, 2017 15:24
-
-
Save davidpaulhunt/1751f6e904f75a050121cd3dcd1eeb7c to your computer and use it in GitHub Desktop.
Basic interface example in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
func write(msg string) { | |
fmt.Println(msg) | |
} | |
type M interface { | |
save() | |
} | |
type U interface { | |
M | |
setSlug() User | |
} | |
type User struct { | |
id int | |
name string | |
slug string | |
} | |
func (u User) save() { | |
write("Saving to the users table!") | |
fmt.Print(u) | |
} | |
func (u User) setSlug() User { | |
u.slug = strings.ToLower(u.name) | |
return u | |
} | |
func main() { | |
var u U = User{id: 1, name: "David"} | |
u = u.setSlug() | |
u.save() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment