有没有一种简单的方法可以在 golang 中创建结构?

我有一个结构,现在想从接收到的 http 数据中实例化它。但是现在自己写的代码比较繁琐,代码行很多。有什么办法可以简化代码吗?除了字段id以外的所有字段都可以对应


模型


type ALiNotifyLog struct {

    ID             *int    `json:"id"`

    APPId          *string `json:"app_id"`

    AuthAppId      *string `json:"auth_app_id"`

    BuyerId        *string `json:"buyer_id"`

    BuyerPayAmount *string `json:"buyer_pay_amount"`

    GmtCreate      *string `json:"gmt_create"`

    GmtPayment     *string `json:"gmt_payment"`

    InvoiceAmount  *string `json:"invoice_amount"`

    NotifyId       *string `json:"notify_id"`

    NotifyTime     *string `json:"notify_time"`

    OutTradeNo     *string `json:"out_trade_no"`

    PointAmount    *string `json:"point_amount"`

    ReceiptAmount  *string `json:"receipt_amount"`

    Sign           *string `json:"sign"`

    TotalAmount    *string `json:"total_amount"`

    TradeNo        *string `json:"trade_no"`

    TradeStatus    *string `json:"trade_status"`

}

功能


func SaveData(data map[string]interface{}) {

    app_id := data["app_id"].(string)

    auth_app_id := data["auth_app_id"].(string)

    buyer_id := data["buyer_id"].(string)

    buyer_pay_amount := data["buyer_pay_amount"].(string)

    gmt_create := data["gmt_create"].(string)

    gmt_payment := data["gmt_payment"].(string)

    invoice_amount := data["invoice_amount"].(string)

    notify_id := data["notify_id"].(string)

    notify_time := data["notify_time"].(string)

    out_trade_no := data["out_trade_no"].(string)

    point_amount := data["point_amount"].(string)

    receipt_amount := data["receipt_amount"].(string)

    sign := data["sign"].(string)

    total_amount := data["total_amount"].(string)

    trade_no := data["trade_no"].(string)

    trade_status := data["trade_status"].(string)

    res := global.Orm.Table(paynotifylog).Create(&model)

    fmt.Println(res)

}


慕桂英546537
浏览 94回答 4
4回答

牛魔王的故事

我看到你是JSON。可以将 JSON 直接解码为结构实例。我将构建类似于以下代码片段的代码:type ALiNotifyLog struct {    // your fields here}func parseRequest(r *http.Request) {    var notifyLog ALiNotifyLog    err := json.NewDecoder(r.Body).Decode(&notifyLog)    if err != nil {        // do something    }    // ............ more code}func SaveData(data ALiNotifyLog) {    res := global.Orm.Table(paynotifylog).Create(&data)    fmt.Println(res)    // ........... more code}

LEATH

我看到你是JSON。可以将 JSON 直接解码为结构实例。我将构建类似于以下代码片段的代码:type ALiNotifyLog struct {    // your fields here}func parseRequest(r *http.Request) {    var notifyLog ALiNotifyLog    err := json.NewDecoder(r.Body).Decode(&notifyLog)    if err != nil {        // do something    }    // ............ more code}func SaveData(data ALiNotifyLog) {    res := global.Orm.Table(paynotifylog).Create(&data)    fmt.Println(res)    // ........... more code}

慕标琳琳

使用来自 GitHub的包mapstructure。go get https://github.com/mitchellh/mapstructurepackage mainimport (    "log"    "os"    "github.com/mitchellh/mapstructure")type MyStruct struct {    This int    That string `json:"thaaaaat"`}func main() {    var result map[string]interface{}    cfg := &mapstructure.DecoderConfig{        TagName: "json",        Result:  &result,    }    decoder, err := mapstructure.NewDecoder(cfg)    if err != nil {        log.Printf("Could not create decoder: %v", err)        os.Exit(1)    }    myData := &MyStruct{        This: 42,        That: "foobar",    }    err = decoder.Decode(myData)    if err != nil {        log.Printf("Decoding failed: %v", err)        os.Exit(1)    }    log.Print(cfg.Result)}输出:&map[This:42 thaaaaaat:foobar]https://go.dev/play/p/mPK_9fEevyC

慕哥6287543

使用来自 GitHub的包mapstructure。go get https://github.com/mitchellh/mapstructurepackage mainimport (    "log"    "os"    "github.com/mitchellh/mapstructure")type MyStruct struct {    This int    That string `json:"thaaaaat"`}func main() {    var result map[string]interface{}    cfg := &mapstructure.DecoderConfig{        TagName: "json",        Result:  &result,    }    decoder, err := mapstructure.NewDecoder(cfg)    if err != nil {        log.Printf("Could not create decoder: %v", err)        os.Exit(1)    }    myData := &MyStruct{        This: 42,        That: "foobar",    }    err = decoder.Decode(myData)    if err != nil {        log.Printf("Decoding failed: %v", err)        os.Exit(1)    }    log.Print(cfg.Result)}输出:&map[This:42 thaaaaaat:foobar]https://go.dev/play/p/mPK_9fEevyC
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go