User Tools

Site Tools


snippets:golang:interface

**This is an old revision of the document!**

interface

snippet.go
package main
 
import "fmt"
 
type A struct {
	A int
}
 
type B struct {
	B int
}
 
func Print(i interface{}) {
	switch v := i.(type) {
	case A:
		fmt.Println("A: ", v.A)
	case B:
		fmt.Println("B: ", v.B)
	}
}
 
func main() {
	a := A{5}
	b := B{42}
 
	Print(a)
	Print(b)
}
A:  5
B:  42
snippets/golang/interface.1521542606.txt.gz · Last modified: by allspark