Skip to content

Instantly share code, notes, and snippets.

@YanhaoYang
Created August 23, 2017 10:28
Show Gist options
  • Save YanhaoYang/dc23744290b98c1e662c41a6696fe7de to your computer and use it in GitHub Desktop.
Save YanhaoYang/dc23744290b98c1e662c41a6696fe7de to your computer and use it in GitHub Desktop.
Array of different types
package main
import (
"fmt"
)
type A struct{}
func (A) hi() string {
return "hi A"
}
type B struct{}
func (B) hi() string {
return "hi B"
}
type hier interface {
hi() string
}
func main() {
arr := []interface{}{&A{}, &B{}}
fmt.Println(arr[0].(hier).hi(), arr[1].(hier).hi())
}
//https://play.golang.org/p/SqKCIEi7Z1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment