Created
August 23, 2017 10:28
-
-
Save YanhaoYang/dc23744290b98c1e662c41a6696fe7de to your computer and use it in GitHub Desktop.
Array of different types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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