无纸记录仪wmdzwsdx接线RO TO GO是什么意思

go - Converting map to struct - Stack Overflow
Join Stack Overflow to learn, share knowledge, and build your career.
or sign in with
I am trying to create a generic method in Go that will fill a struct using data from a map[string]interface{}. For example, the method signature and usage might look like:
func FillStruct(data map[string]interface{}, result interface{}) {
type MyStruct struct {
Name string
myData := make(map[string]interface{})
myData["Name"] = "Tony"
myData["Age"]
result := &MyStruct{}
FillStruct(myData, result)
// result now has Name set to "Tony" and Age set to 23
I know this can be done using JSON is there another more efficient way of doing this?
This is the same idea as Simon's answer, but with a little more error handling:
func SetField(obj interface{}, name string, value interface{}) error {
structValue := reflect.ValueOf(obj).Elem()
structFieldValue := structValue.FieldByName(name)
if !structFieldValue.IsValid() {
return fmt.Errorf("No such field: %s in obj", name)
if !structFieldValue.CanSet() {
return fmt.Errorf("Cannot set %s field value", name)
structFieldType := structFieldValue.Type()
val := reflect.ValueOf(value)
if structFieldType != val.Type() {
return errors.New("Provided value type didn't match obj field type")
structFieldValue.Set(val)
return nil
type MyStruct struct {
Name string
func (s *MyStruct) FillStruct(m map[string]interface{}) error {
for k, v := range m {
err := SetField(s, k, v)
if err != nil {
return err
return nil
func main() {
myData := make(map[string]interface{})
myData["Name"] = "Tony"
myData["Age"] = int64(23)
result := &MyStruct{}
err := result.FillStruct(myData)
if err != nil {
fmt.Println(err)
fmt.Println(result)
27.5k12658
Hashicorp's
library does this out of the box:
import "github.com/mitchellh/mapstructure"
mapstructure.Decode(myData, &result)
The second result parameter has to be an address of the struct.
You can do it ... it may get a bit ugly and you'll be faced with some trial and error in terms of mapping types .. but heres the basic gist of it:
func FillStruct(data map[string]interface{}, result interface{}) {
t := reflect.ValueOf(result).Elem()
for k, v := range data {
val := t.FieldByName(k)
val.Set(reflect.ValueOf(v))
Working sample:
I adapt dave's answer, and add a recursive feature. I'm still working on a more user friendly version. For example, a number string in the map should be able to be converted to int in the struct.
package main
func SetField(obj interface{}, name string, value interface{}) error {
structValue := reflect.ValueOf(obj).Elem()
fieldVal := structValue.FieldByName(name)
if !fieldVal.IsValid() {
return fmt.Errorf("No such field: %s in obj", name)
if !fieldVal.CanSet() {
return fmt.Errorf("Cannot set %s field value", name)
val := reflect.ValueOf(value)
if fieldVal.Type() != val.Type() {
if m,ok := value.(map[string]interface{}); ok {
// if field value is struct
if fieldVal.Kind() == reflect.Struct {
return FillStruct(m, fieldVal.Addr().Interface())
// if field value is a pointer to struct
if fieldVal.Kind()==reflect.Ptr && fieldVal.Type().Elem().Kind() == reflect.Struct {
if fieldVal.IsNil() {
fieldVal.Set(reflect.New(fieldVal.Type().Elem()))
// fmt.Printf("recursive: %v %v\n", m,fieldVal.Interface())
return FillStruct(m, fieldVal.Interface())
return fmt.Errorf("Provided value type didn't match obj field type")
fieldVal.Set(val)
return nil
func FillStruct(m map[string]interface{}, s interface{}) error {
for k, v := range m {
err := SetField(s, k, v)
if err != nil {
return err
return nil
type OtherStruct struct {
Name string
type MyStruct struct {
Name string
OtherStruct *OtherStruct
func main() {
myData := make(map[string]interface{})
myData["Name"]
myData["Age"]
= int64(23)
OtherStruct := make(map[string]interface{})
myData["OtherStruct"] = OtherStruct
OtherStruct["Name"]
OtherStruct["Age"]
= int64(23)
result := &MyStruct{}
err := FillStruct(myData,result)
fmt.Println(err)
fmt.Printf("%v %v\n",result,result.OtherStruct)
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled一首英文歌曲。歌词大概是you can go wherever you want to go_百度知道
一首英文歌曲。歌词大概是you can go wherever you want to go
you can go wherever you want to(wanna) go ..... you can be whatever you want to be 在超市里听到的,是女声的歌词
不是Be what you wanna be,也不是what will be will be
我有更好的答案
丁可的if,你搜搜听听看,我找了一晚上,一听就喜欢上了。
采纳率:33%
如果你实在找不到,试试Something Just Like This - The Chainsmokers / ColdplayShe said where'd you wanna goHow much you wanna risk
楼楼,你找到了嘛?
we can't stopMiley Cyrus的歌曲,你听听是不是
终于找到你了
虽然没找到你说的 但我推荐你一个吧 来自星星的主题曲my destiny 愿你早日找到
stay here forever
其他21条回答
为您推荐:
其他类似问题
英文歌曲的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。IronKey(TM) Workspace | Windows To Go | IronKey
Hello and Welcome to IronKey.
If you have not already heard the good news, Kingston Digital has acquired the USB technology and assets of IronKey&
from . Kingston's goal is to ensure a smooth transition for existing IronKey customers and partners & enabling
access to products, sales and world class technical support services. Kingston will also leverage its longstanding
relationships with Flash memory semiconductors, controller partners and other component makers & as well as our strong
channel presence & to bring IronKey products into the future.
In addition, encryption services leader
has purchased the IronKey
(EMS) platform which provides centralized management to encrypted
USB drives, and has also acquired the IronKey encrypted hard drive business.
will continue
to manage the
platforms that both Kingston and
IronKey managed encrypted drives utilize.
For product or support information, please click on the product pages and the links will direct you to
either Kingston or DataLocker for additional information.
Thank You.
IronKey Workspace with Windows To Go is a PC on a Stick& that equips employees and contractors with a portable Windows 10 or 8.1 corporate image.
This Microsoft-certified solution comes complete with your applications, security controls and access policies stored on a ruggedized, fully manageable USB flash drive & all at savings of up to 95% compared to the cost of laptops.
Microsoft-Certified for
IronKey Workspace W700 wins Best of Tech Ed 2014!
Boot a fully functioning Microsoft Windows 10 or 8.1 desktop directly from a trusted Imation IronKey USB flash drive with hardware encryption and optional centralized management.
IronKey Workspace W500 is PC Magazine's new Editor's Choice for Windows To Go devices
Boot a fully functioning Microsoft Windows 10 or 8.1 desktop directly from a trusted Imation IronKey USB flash drive with 256-bit AES full disk encryption leveraging Microsoft BitLocker technology.
IronKey Workspace W300 is PC Magazine's new Editor's Choice for Windows To Go devices豆丁微信公众号
君,已阅读到文档的结尾了呢~~
虹润大屏幕液晶汉显无纸记录仪列入福建省重点科技项虹润大屏
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
[精品]虹润大屏幕液晶汉显无纸记录仪列入福建省重点科技项...
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='http://www.docin.com/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口

我要回帖

更多关于 无纸记录仪wmdzwzjly 的文章

 

随机推荐