Skip to content

Instantly share code, notes, and snippets.

@davidpaulhunt
Last active September 10, 2017 15:24
Show Gist options
  • Save davidpaulhunt/1751f6e904f75a050121cd3dcd1eeb7c to your computer and use it in GitHub Desktop.
Save davidpaulhunt/1751f6e904f75a050121cd3dcd1eeb7c to your computer and use it in GitHub Desktop.
Basic interface example in Go
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