Skip to content

Instantly share code, notes, and snippets.

@ttacon
Last active August 29, 2015 14:10
Show Gist options
  • Save ttacon/55fdf19b5b9fcfb7780f to your computer and use it in GitHub Desktop.
Save ttacon/55fdf19b5b9fcfb7780f to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"reflect"
)
func main() {
a := 0
b := 1
ipntrs := []*int{&a, &b}
vals := []interface{}{ipntrs[0], ipntrs[1], "hello world!"}
fmt.Println("a is now: ", a)
fmt.Println("b is now: ", b)
fmt.Println("-----")
interfaceVarArgs(vals...)
fmt.Println("-----")
fmt.Println("a is now: ", a)
fmt.Println("b is now: ", b)
// Output:
//
// a is now: 0
// b is now: 1
// -----
// value 0 is: 0
// value 1 is: 1
// value 2 is: hello world!
// -----
// a is now: 1
// b is now: 2
}
func interfaceVarArgs(ifaces ...interface{}) {
for i, iface := range ifaces {
if reflect.TypeOf(iface).Kind() == reflect.Ptr {
fmt.Printf("value %d is: %v\n", i, reflect.ValueOf(iface).Elem().Interface())
} else {
fmt.Printf("value %d is: %v\n", i, iface)
}
if ipntr, ok := iface.(*int); ok {
*ipntr = i + 1
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment