Last active
October 6, 2017 06:24
-
-
Save keitaj/09c959c9102f3d27c2d2 to your computer and use it in GitHub Desktop.
Go言語でstructをmapに変換する ref: http://qiita.com/keitaj/items/440a50a53c8980ee338f
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" | |
"reflect" | |
"encoding/json" | |
) | |
func StructToMap(data interface{}) map[string]interface{} { | |
result := make(map[string]interface{}) | |
elem := reflect.ValueOf(data).Elem() | |
size := elem.NumField() | |
for i := 0; i < size; i++ { | |
field := elem.Type().Field(i).Name | |
value := elem.Field(i).Interface() | |
result[field] = value | |
} | |
return result | |
} | |
func StructToJsonTagMap(data interface{}) map[string]interface{} { | |
result := make(map[string]interface{}) | |
elem := reflect.ValueOf(data).Elem() | |
size := elem.NumField() | |
for i := 0; i < size; i++ { | |
field := elem.Type().Field(i).Tag.Get("json") | |
value := elem.Field(i).Interface() | |
result[field] = value | |
} | |
return result | |
} | |
func StructToJsonTagMap2(data interface{}) map[string]interface{} { | |
result := make(map[string]interface{}) | |
b, _ := json.Marshal(data) | |
json.Unmarshal(b, &result) | |
return result | |
} | |
type A struct { | |
ID int `json:"id"` | |
Name string `json:"name"` | |
} | |
func main() { | |
a := A{1, "keitaj"} | |
fmt.Println(a) | |
b := StructToMap(&a) | |
fmt.Println(b) | |
c := StructToJsonTagMap(&a) | |
fmt.Println(c) | |
d := StructToJsonTagMap2(&a) | |
fmt.Println(d) | |
} |
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" | |
"reflect" | |
"encoding/json" | |
) | |
func StructToMap(data interface{}) map[string]interface{} { | |
result := make(map[string]interface{}) | |
elem := reflect.ValueOf(data).Elem() | |
size := elem.NumField() | |
for i := 0; i < size; i++ { | |
field := elem.Type().Field(i).Name | |
value := elem.Field(i).Interface() | |
result[field] = value | |
} | |
return result | |
} | |
func StructToJsonTagMap(data interface{}) map[string]interface{} { | |
result := make(map[string]interface{}) | |
elem := reflect.ValueOf(data).Elem() | |
size := elem.NumField() | |
for i := 0; i < size; i++ { | |
field := elem.Type().Field(i).Tag.Get("json") | |
value := elem.Field(i).Interface() | |
result[field] = value | |
} | |
return result | |
} | |
func StructToJsonTagMap2(data interface{}) map[string]interface{} { | |
result := make(map[string]interface{}) | |
b, _ := json.Marshal(data) | |
json.Unmarshal(b, &result) | |
return result | |
} | |
type A struct { | |
ID int `json:"id"` | |
Name string `json:"name"` | |
} | |
func main() { | |
a := A{1, "keitaj"} | |
fmt.Println(a) | |
b := StructToMap(&a) | |
fmt.Println(b) | |
c := StructToJsonTagMap(&a) | |
fmt.Println(c) | |
d := StructToJsonTagMap2(&a) | |
fmt.Println(d) | |
} |
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
{1 keitaj} | |
map[ID:1 Name:keitaj] | |
map[id:1 name:keitaj] | |
map[name:keitaj id:1] |
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
{1 keitaj} | |
map[ID:1 Name:keitaj] | |
map[id:1 name:keitaj] | |
map[name:keitaj id:1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment