Skip to content

Instantly share code, notes, and snippets.

@schmurfy
Created July 24, 2020 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schmurfy/8a476fa003fde0f41de0a91b13003502 to your computer and use it in GitHub Desktop.
Save schmurfy/8a476fa003fde0f41de0a91b13003502 to your computer and use it in GitHub Desktop.
package main
import "fmt"
type User struct {
Name string
Age int
}
// func (u *User) GetName() string {
// return u.Name
// }
func (u *User) GetAge() int {
return u.Age
}
type Named interface {
GetName() string
}
type Aged interface {
GetAge() int
}
func main() {
u1 := &User{Name: "John", Age: 32}
var i interface{} = u1
aged, ok := i.(Aged)
if ok {
fmt.Printf("age: %d\n", aged.GetAge())
named, ok := aged.(Named)
if ok {
fmt.Printf("name: %s\n", named.GetName())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment