Skip to content

Instantly share code, notes, and snippets.

@cevaris
Last active January 24, 2018 09:59
Show Gist options
  • Save cevaris/e0ad30882370dd8d2e09 to your computer and use it in GitHub Desktop.
Save cevaris/e0ad30882370dd8d2e09 to your computer and use it in GitHub Desktop.
Golang function with interface as argument
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)
}
@cevaris
Copy link
Author

cevaris commented Mar 2, 2015

Output

stanley
david
oscar
11111
222222

33333

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment