第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

在編組為 JSON 之前將結(jié)構(gòu)轉(zhuǎn)換為不同的結(jié)構(gòu)

在編組為 JSON 之前將結(jié)構(gòu)轉(zhuǎn)換為不同的結(jié)構(gòu)

Go
月關(guān)寶盒 2022-06-27 17:01:35
我正在嘗試使用圍棋作為后端來實現(xiàn)棋盤游戲,并且遇到了一些障礙,并且試圖了解以圍棋方式解決此問題的最佳方法是什么。我有一個復雜的結(jié)構(gòu),代表我在游戲引擎中使用的游戲狀態(tài),以評估狀態(tài)以及每個動作將做什么以及它將如何影響游戲。type Game struct {    ID            string    Name          string              `json:"name"`    Version       Version             `json:"version"`    StartingMode  StartingMode        `json:"startingMode"`    Players       []*Player           `json:"players"`    GameMap       *BoardMap           `json:"gameMap"`    Countries     map[string]*country `json:"countries"`    Config        *Config             `json:"config"`    TurnPlayer    *Player             `json:"turnPlayer"`    //represents the player who started the turn    TurnCountry   *country            `json:"turnCountry"`   //represents the country which started the turn    CurrentPlayer *Player             `json:"currentPlayer"` //represents the current player to action    GameStart    bool     `json:"gameStart"`    GameFinish   bool     `json:"gameFinish"`    InAuction    bool     `json:"inAuction"` //represents the game is in the auction stage    GameSettings Settings `json:"settings"`}現(xiàn)在,我可以將它編組為 JSON 并將其保存在我的數(shù)據(jù)庫中,它工作正常,但是當我必須將它發(fā)送到前端時,它并不能真正工作。前端也不需要知道那么多信息,我真的想要一些更簡單的東西,例如:type OtherGame struct { players []*OtherPlayer countries []*OtherCountry map []*OtherArea}所以,我想我必須編寫一些轉(zhuǎn)換器函數(shù),然后編組這個 OtherGame 結(jié)構(gòu),或者我應該編寫一個自定義函數(shù),迭代到 Game 中的不同結(jié)構(gòu)并使用 Sprintf 將它們放入字符串中?
查看完整描述

2 回答

?
眼眸繁星

TA貢獻1873條經(jīng)驗 獲得超9個贊

我經(jīng)常使用這種設(shè)計模式來為特定的處理程序生成自定義輸出。在那里定義您的 JSON 標簽,并且只公開您需要的內(nèi)容。然后自定義類型與您的處理程序緊密耦合:


func gameHandler(w http.ResponseWriter, r *http.Request) {

    g, err := dbLookupGame(r) // handle err


    // define one-time anonymous struct for custom formatting

    jv := struct {

        ID      string `json:"id"`

        Name    string `json:"name"`

        Version string `json:"ver"`

    }{

        ID:      g.ID,

        Name:    g.Name,

        Version: g.Version,

    }


    err = json.NewEncoder(w).Encode(&jv) // handle err

}


type game struct {

    ID      string

    Name    string

    Version string

    Secret  string // don't expose this

   // other internals ...

}

https://play.golang.org/p/hhAAlmb51Ue


查看完整回答
反對 回復 2022-06-27
?
慕運維8079593

TA貢獻1876條經(jīng)驗 獲得超5個贊

您可以編寫一個method將您的Game數(shù)據(jù)轉(zhuǎn)換為OtherGame. 像這樣的東西。


func (game Game) OtherGame() OtherGame {

    return OtherGame{

        players:   game.Players,

        countries: game.Countries,

    }

}

在發(fā)送到 之前調(diào)用此方法front-end。


game := Game{...}

otherGame := game.OtherGame()


查看完整回答
反對 回復 2022-06-27
  • 2 回答
  • 0 關(guān)注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號