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

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

遍歷 golang 中的一系列 struts

遍歷 golang 中的一系列 struts

Go
繁星coding 2022-11-23 15:28:29
我的術語可能不適用,所以我使用了一些 python 術語。在 golang 中嘗試做的是迭代一個具有這樣存儲值的支柱(這是我從 api 返回的)[{STOREA 0 0 0} {STOREB 0 0 0} {STOREC 0 0 0} {STORED 0 0 0}]在 python 中,我將其稱為字典列表。當我將鼠標懸停在可視代碼中的值上時,它指出:field itemData []struct{代碼字符串 "json:"Code""; 項目 int "json:"項目""; 價格 float64 "json:"價格""; 數量 int "json:"數量""}package main// I think this is the right way to interpret what visual code statedtype itemData struct {    Code string  `json:"Code"`    Items int     `json:"Items"`    Price   float64 `json:"Price"`    Qty int     `json:"Qty"`    }//I'm trying to simulate the api response by storing it in a varible like this but I think the [] are an issue and im doing it incorrectlyvar storeData = itemData[{STOREA 0 0 0} {STOREB0 0 0} {STOREC 0 0 0} {STORED0 0 0}] // I think the [ ] brackets are causing an issue//The idea is I can iterate over it like this:for k,v := range storeData {    fmt.Printf(k,v)    }
查看完整描述

2 回答

?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

推薦給Go 之旅 - Slice


然后試試這個:


    var storeData = []itemData{{"STOREA", 0, 0, 0}, {"STOREB", 0, 0, 0}, {"STOREC", 0, 0, 0}, {"STORED", 0, 0, 0}}

    for k, v := range storeData {

        fmt.Println(k, v)

    }

https://go.dev/play/p/aRaJlqKe00L


查看完整回答
反對 回復 2022-11-23
?
函數式編程

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

你所擁有的slice是itemData( struct)。使用切片文字用值初始化切片非常簡單。它看起來像這樣:


    storeData := []itemData{

        {

            Code:  "STOREA",

            Items: 0,

            Price: 0,

            Qty:   0,

        },

        {

            Code:  "STOREB0",

            Items: 0,

            Price: 0,

        },

        {

            Code:  "STOREC",

            Items: 0,

            Price: 0,

            Qty:   0,

        },

        {

            Code:  "STORED0",

            Items: 0,

            Price: 0,

        },

    }

    for i, v := range storeData {

        fmt.Printf("index: %d, value: %v\n", i, v)

    }


查看完整回答
反對 回復 2022-11-23
  • 2 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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