douren9077 2018-04-17 05:22
浏览 32
已采纳

如何在单个函数golang中从不同文件解码两个不同的结构?

I want to decode the two different struct for saving the data into the collection. but it gives error.

The two struct are given below these two files are in the folder named models.

User.go

type User struct {
 Id                         int         `json:"id" bson:"_id,omitempty"`
 FirstName                  string      `json:"first_name,omitempty" bson:"first_name,omitempty"`
 LastName                   string      `json:"last_name,omitempty" bson:"last_name,omitempty"`
 EmailId                    string      `json:"email_id,omitempty" bson:"email_id,omitempty"`
 Password                   string      `json:"password,omitempty" bson:"password,omitempty"`
 PhoneNumber                string      `json:"phone_number,omitempty" bson:"phone_number,omitempty"`
 AltPhoneNumber             string      `json:"alt_phone_number" bson:"alt_phone_number"`
 Gender                     string      `json:"gender,omitempty" bson:"gender,omitempty"`
 Note                       string      `json:"note,omitempty" bson:"note,omitempty"`
}

Provider.go

type Provider struct {
 Id                      int                `json:"id" bson:"_id,omitempty"`
 Uid                     int                `json:"uid" bson:"uid,omitempty"`
 Speed                   string             `json:"speed,omitempty" bson:"speed,omitempty"`
 ProviderCategory        string             `json:"provider_category,omitempty" bson:"provider_category,omitempty"`
 Priority                int                `json:"priority,omitempty" bson:"priority,omitempty"`
}

The code I tried to decode them

import (
    "encoding/json"
    "go-training/models"
    "github.com/gin-gonic/gin"
    "fmt"
)

var provider models.Provider
var user models.User

func SaveProvider(c *gin.Context) {
    response := ResponseController{}
    //decode the struct
    providerErr := json.NewDecoder(c.Request.Body).Decode(&provider)
    fmt.Println(providerErr) //Output:- nil
    userErr := json.NewDecoder(c.Request.Body).Decode(&user)
    fmt.Println(userErr) //output:- EOF
    if userErr != nil {
        fmt.Println("data is not properly formatted")
    }
    if providerErr != nil {
        fmt.Printl?("data is not properly formatted")
    }
}

Data entered in the json form-

{
 "Id":1,
 "Uid":1,
 "speed":"new",
 "provider_category":"WL",
 "priority":1,
 "first_name":"puneet",
 "last_name":"jindal",
 "email_id":"puneet@gmail.com",
 "password":"poiuytrewq",
 "phone_number":"9876543210",
 "alt_phone_number":"9876543210",
 "gender":"male",
 "note":"phase 7"
}    

Error:- EOF

How can I solve this problem? Can anyone help me? Thank you.

  • 写回答

1条回答 默认 最新

  • douseda0009 2018-04-17 06:10
    关注

    The code in the question does not work because the first decoder reads the entire request body. The second decoder starts at EOF.

    The fix is to slurp up the body to a byte slice and unmarshal twice from that slice:

    // slurp up the body.  p is a []byte
    p, err := ioutil.ReadAll(c.Request.Body)
    if err != nil {
       // handler error
    }
    
    var provider models.Provider
    if err := json.Unmarshal(p, &provider); err != nil {
        // handle error
    }
    
    var user models.User
    if err := json.Unmarshal(p, &user); err != nil {
         // handle error
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退