Last active
January 24, 2018 09:59
-
-
Save cevaris/e0ad30882370dd8d2e09 to your computer and use it in GitHub Desktop.
Golang function with interface as argument
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" | |
// Play | |
// http://play.golang.org/p/Hs59f-DNXp | |
func PrintAll(vals interface{}) { | |
switch v := vals.(type) { | |
case []string: | |
for _, val := range v { | |
fmt.Println(val) | |
} | |
case []int: | |
for _, val := range v { | |
fmt.Println(val) | |
} | |
} | |
} | |
func main() { | |
names := []string{"stanley", "david", "oscar"} | |
PrintAll(names) | |
namesInts := []int{11111, 222222, 33333} | |
PrintAll(namesInts) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output
33333