初始化地图问题

我正在开发一个项目,我需要声明以下内容:


mapDataPayload := make(map[string][]*dataPayload)

如果我向它附加数据,它会正常工作。


mapDataPayload := make(map[string][]*dataPayload)


for {

    select {

    case rcvData := <-ch:


        mapDataPayload[rcvData.Topic] = append(

            mapDataPayload[rcvData.Topic],

            &dataPayload{Message: rcvData.Message},

        )

    }

}

但是,我想设置一个大小限制。随着追加,它会不停地增长。我想要实现的是当达到限制(最大值:100)时,它会覆盖索引 0、1、2 ...


mapDataPayload[rcvData.Topic][0]

mapDataPayload[rcvData.Topic][1]

我尝试用以下方法初始化以下内容:


make(map[string][]*dataPayload, 100)


for {

    select {

    case rcvData := <-ch:

        mapDataPayload[rcvData.Topic][0] = &dataPayload{Message: rcvData.Message}

    }

}

但是,如果我检查长度,它将返回 0。用直接初始化 (mapDataPayload[rcvData.Topic][0]) 替换 append 会立即导致错误。


所以,我想要做的是用一个限制填充 map[string][]*dataPayload,例如这个数据:


{

  "test1": {

    "0": {

      "Message": "Heasdllo"

    },

    "1": {

      "Message": "Hel132lo"

    },

    "2": {

      "Message": "Hedsallo"

    }

  },

  "testanother": {

    "0": {

      "Message": "adsad"

    },

    "1": {

      "Message": "Helwqe2lo"

    },

    "2": {

      "Message": "Hel21321lo"

    },

    "3": {

      "Message": "Hel21321lo"

    }

  }

}

当它达到数字 100 时,我想回到数字 0、1、2...


波斯汪
浏览 95回答 2
2回答

慕尼黑5688855

您可以像这样为循环缓冲区实现数据结构package mainimport (&nbsp; &nbsp; "fmt")type Circle struct&nbsp; {&nbsp; &nbsp;Size int&nbsp; &nbsp;Contents []interface{}&nbsp; &nbsp;Pointer int}func (c *Circle) Setup(n int) {&nbsp; &nbsp;(*c).Size=n&nbsp; &nbsp;(*c).Contents=make([]interface{},n)}func (c *Circle) Add(value interface{}) {&nbsp; &nbsp;(*c).Contents[(*c).Pointer] = value&nbsp; &nbsp;(*c).Pointer = ((*c).Pointer+1) % (*c).Size}func main() {&nbsp; &nbsp; &nbsp; &nbsp; mapDataPayload := make(map[string]*Circle)&nbsp; &nbsp; &nbsp; &nbsp; mapDataPayload["aaa"]=&Circle{}&nbsp; &nbsp; &nbsp; &nbsp; mapDataPayload["aaa"].Setup(10)&nbsp; &nbsp; for i:=0; i<9999; i++ {&nbsp; &nbsp; &nbsp; mapDataPayload["aaa"].Add(i)&nbsp; &nbsp; }&nbsp; &nbsp; mapDataPayload["aaa"].Add("banana")&nbsp; &nbsp; fmt.Println(mapDataPayload["aaa"].Contents)}更新为使用 interface{} 以便可以使用任何类型的数据

aluckdog

像这样使用:for {&nbsp; &nbsp; select {&nbsp; &nbsp; case rcvData := <-ch:&nbsp; &nbsp; &nbsp; &nbsp; _, exists := mapDataPayload[rcvData.Topic]&nbsp; &nbsp; &nbsp; &nbsp; if !exists {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mapDataPayload[rcvData.Topic] = []*dataPayload{}&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if len(mapDataPayload[rcvData.Topic]) < 100 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mapDataPayload[rcvData.Topic] = append(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mapDataPayload[rcvData.Topic],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &dataPayload{Message: rcvData.Message},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mapDataPayload[rcvData.Topic][len(mapDataPayload[rcvData.Topic]) % 100] = &dataPayload{Message: rcvData.Message}&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go