User Tools

Site Tools


snippets:golang:interface

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)
}

output

Click to display ⇲

Click to hide ⇱

A:  5
B:  42
snippets/golang/interface.txt · Last modified: by allspark