Last active
December 7, 2016 10:24
-
-
Save rodkranz/b5bb0d95d5f3f34e56248d2ce377b226 to your computer and use it in GitHub Desktop.
func with params dynamic
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
// 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