初级会员
  • 第 5486 位会员
  • luominggao123
  • 519446373@qq.com
  • 2016-06-24 03:39:42
  • Offline
  • 22 39

最近发布的文章

最近分享的资源

    暂无

最近发布的项目

    暂无

最近的评论

  • ``` //把xml文件保存为version.xml package main import ( "encoding/xml" "fmt" "io/ioutil" "log" "os" ) type Context struct { XMLName xml.Name Body Body } type Body struct { XMLName xml.Name Login Login `xml:"login"` } type Login struct { XMLName xml.Name `xml:"login"` Id string `xml:"xmlns,attr"` Inner Inner `xml:"struct"` } type Inner struct { XMLName xml.Name `xml:"struct"` UserName string `xml:"username"` Password string `xml:"password"` } func main() { content, err := ioutil.ReadFile("version.xml") if err != nil { log.Fatal(err) } res := new(Context) err = xml.Unmarshal(content, res) res.Body.Login.Inner.UserName = "你想修改的username1" res.Body.Login.Inner.Password = "你想修改的password1" xmlOutPut, outPutErr := xml.MarshalIndent(res, "", "") if outPutErr == nil { //加入XML头 headerBytes := []byte(xml.Header) //拼接XML头和实际XML内容 xmlOutPutData := append(headerBytes, xmlOutPut...) //写入文件 ioutil.WriteFile("version.xml", xmlOutPutData, os.ModeAppend) fmt.Println("OK~") } else { fmt.Println(outPutErr) } } ```
  • ``` package main import ( "bytes" "encoding/gob" "errors" "fmt" "reflect" "strings" ) func main() { mList := []map[string]interface{}{ {"Id": 213, "Name": "zhaoliu", "Sex": "男"}, {"Id": 56, "Name": "zhangsan", "Sex": "男"}, {"Id": 7, "Name": "lisi", "Sex": "女"}, {"Id": 978, "Name": "wangwu", "Sex": "男"}, } type User struct { Id int Name string Sex string } users := []*User{} mapToStruct(mList, &users) fmt.Printf("users: %+v\n", users) } func mapToStruct(mList []map[string]interface{}, model interface{}) (err error) { val := reflect.Indirect(reflect.ValueOf(model)) typ := val.Type() for _, r := range mList { mVal := reflect.Indirect(reflect.New(typ.Elem().Elem())).Addr() for key, val := range r { err = setField(mVal.Interface(), key, val) if err != nil { return err } } val = reflect.Append(val, mVal) } DeepCopy(model, val.Interface()) return err } //用map的值替换结构的值 func setField(obj interface{}, name string, value interface{}) error { // 将首字母转换为大写 sl := strings.Split(name, "") sl[0] = strings.ToUpper(sl[0]) name = strings.Join(sl, "") structValue := reflect.ValueOf(obj).Elem() //结构体属性值 //fmt.Printf("structValue: %+v\n", structValue) structFieldValue := structValue.FieldByName(name) //结构体单个属性值 //fmt.Printf("structFieldValue: %+v\n", structFieldValue) 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) //map值的反射值 if structFieldType != val.Type() { return errors.New("type is err") } structFieldValue.Set(val) return nil } func DeepCopy(dst, src interface{}) error { var buf bytes.Buffer if err := gob.NewEncoder(&buf).Encode(src); err != nil { return err } return gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(dst) } ```
  • 好吧,我的理解错了。
  • ``` package main import ( "errors" "fmt" "reflect" "strings" ) type User struct { Id int Name string Sex string } var users []*User func main() { mList := []map[string]interface{}{ {"Id": 213, "Name": "zhaoliu", "Sex": "男"}, {"Id": 56, "Name": "zhangsan", "Sex": "男"}, {"Id": 7, "Name": "lisi", "Sex": "女"}, {"Id": 978, "Name": "wangwu", "Sex": "男"}, } mapToStruct(mList, &users) fmt.Printf("users: %+v\n", users[0]) } func mapToStruct(mList []map[string]interface{}, model interface{}) (err error) { val := reflect.Indirect(reflect.ValueOf(model)) typ := val.Type() for _, r := range mList { mVal := reflect.Indirect(reflect.New(typ.Elem().Elem())).Addr() for key, val := range r { err = setField(mVal.Interface(), key, val) if err != nil { return err } } v, ok := mVal.Interface().(*User) if ok { users = append(users, v) } } return err } //用map的值替换结构的值 func setField(obj interface{}, name string, value interface{}) error { // 将首字母转换为大写 sl := strings.Split(name, "") sl[0] = strings.ToUpper(sl[0]) name = strings.Join(sl, "") structValue := reflect.ValueOf(obj).Elem() //结构体属性值 //fmt.Printf("structValue: %+v\n", structValue) structFieldValue := structValue.FieldByName(name) //结构体单个属性值 //fmt.Printf("structFieldValue: %+v\n", structFieldValue) 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) //map值的反射值 if structFieldType != val.Type() { return errors.New("type is err") } structFieldValue.Set(val) return nil } ```
  • 评论了主题 麻将胡牌算法
    #5 @EddieChan1993 那希望大家帮忙看看