亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有一種簡單的方法可以在 golang 中創建結構?

有沒有一種簡單的方法可以在 golang 中創建結構?

Go
慕桂英546537 2022-12-26 10:30:49
我有一個結構,現在想從接收到的 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)}
查看完整描述

4 回答

?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

我看到你是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

}


查看完整回答
反對 回復 2022-12-26
?
LEATH

TA貢獻1936條經驗 獲得超7個贊

我看到你是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

}


查看完整回答
反對 回復 2022-12-26
?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

使用來自 GitHub的mapstructure。


go get https://github.com/mitchellh/mapstructure

package main


import (

    "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


查看完整回答
反對 回復 2022-12-26
?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

使用來自 GitHub的包mapstructure。


go get https://github.com/mitchellh/mapstructure

package main


import (

    "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


查看完整回答
反對 回復 2022-12-26
  • 4 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號