-
-
Save sofianhw/c8d3097554bd124acfccc45d7649fdfb to your computer and use it in GitHub Desktop.
Golang: Converting a struct to a map (to a url.Values string map)
This file contains 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" | |
"net/url" | |
"reflect" | |
"strconv" | |
) | |
type Person struct { | |
Name string | |
Age uint | |
} | |
func main() { | |
betty := Person{ | |
Name: "Betty", | |
Age: 82, | |
} | |
urlValues := structToMap(&betty) | |
fmt.Printf("%#v", urlValues) | |
// Response... | |
// url.Values{"Name":[]string{"Betty"}, "Age":[]string{"82"}} | |
// It works! Play with it @ http://play.golang.org/p/R8jhzfI_ac | |
} | |
func structToMap(i interface{}) (values url.Values) { | |
values = url.Values{} | |
iVal := reflect.ValueOf(i).Elem() | |
typ := iVal.Type() | |
for i := 0; i < iVal.NumField(); i++ { | |
f := iVal.Field(i) | |
// You ca use tags here... | |
// tag := typ.Field(i).Tag.Get("tagname") | |
// Convert each type into a string for the url.Values string map | |
var v string | |
switch f.Interface().(type) { | |
case int, int8, int16, int32, int64: | |
v = strconv.FormatInt(f.Int(), 10) | |
case uint, uint8, uint16, uint32, uint64: | |
v = strconv.FormatUint(f.Uint(), 10) | |
case float32: | |
v = strconv.FormatFloat(f.Float(), 'f', 4, 32) | |
case float64: | |
v = strconv.FormatFloat(f.Float(), 'f', 4, 64) | |
case []byte: | |
v = string(f.Bytes()) | |
case string: | |
v = f.String() | |
} | |
values.Set(typ.Field(i).Name, v) | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment