Skip to content

Instantly share code, notes, and snippets.

@rodkranz
Last active December 7, 2016 10:24
Show Gist options
  • Save rodkranz/b5bb0d95d5f3f34e56248d2ce377b226 to your computer and use it in GitHub Desktop.
Save rodkranz/b5bb0d95d5f3f34e56248d2ce377b226 to your computer and use it in GitHub Desktop.
func with params dynamic
// Copyright 2016 Kranz. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"reflect"
)
func TypeOf(vars ...interface{}) {
for i, v := range vars {
switch reflect.TypeOf(v).Kind() {
case reflect.Slice:
s := reflect.ValueOf(v)
fmt.Printf("The param number %v is a SLICE and their values are: ", i)
for j := 0; j < s.Len(); j++ {
fmt.Printf("%q", s.Index(j))
if j < s.Len() - 1 {
fmt.Print(", ")
}
}
fmt.Println()
case reflect.String:
s := reflect.ValueOf(v)
fmt.Printf("The param number %v is a STRING and its values are: %v\n", i, s.String())
case reflect.Struct:
s := reflect.ValueOf(v)
fmt.Printf("The param number %v is a STRUCT and its values are: %v\n", i, s)
}
}
}
type A struct {
name string
age int
}
func main() {
TypeOf("Golang", []string{"a", "slice", "anything"}, A{name: "Rodrigo", age: 29})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment