猿问

使用嵌套映射连接字符串时无法获得适当的输出

我是 galang 的新手,我想用嵌套地图连接字符串。下面是给出的虚拟代码,指出我的错误。提前致谢


    import (

    "fmt"

    "strconv"

    )


    func main() {

    str := "Hello @John martin #sosos &Hi @William "

    var key string = ""

    var ku int = 0

    var kh int = 0

    var kg int = 0

    var id string

    var col string 

    var retMap = make(map[string]map[string]string)

    retMap[key] = make(map[string]string)    

    for i := 0; i < len(str); i++ {

      //fmt.Println(string(str[i]), " >> ", str[i])

      if str[i] == 64 || str[i] == 35 || str[i] == 38 {

        if str[i] == 64 {

          ku++

          key = "user"

          id = strconv.Itoa(ku)

      } else if str[i] == 35 {

        kh++

        key = "hashTag"

        id = strconv.Itoa(kh)

      } else {

        kg++

        key = "group"

        id = strconv.Itoa(kg)

      }

      retMap[key] = make(map[string]string) // If not assign here then it gives runtime error "panic: assignment to entry in nil map"

      for j := i + 1; j < len(str); j++ {

        col = col + string(str[j])

        if str[j] == 32 {

          j = len(str) - 1

          retMap[key][id] = retMap[key][id] + col

          col = " "

        }

      }

    }

 }

 fmt.Println("Final String ",retMap)

}

输出:最终字符串 map[group:map[1: Hi] user:map[2: William] hashTag:map[1: sosos]]


预期输出:


最终字符串 map[group:map[1: Hi] user:map[1: John, 2: William] hashTag:map[1: sosos ]]


它可能是重复的


慕慕森
浏览 142回答 1
1回答

潇湘沐

问题是您正在每个调用中初始化“key”的嵌套映射,因此它始终覆盖以前的数据。所以只需改变你的线路retMap[key] = make(map[string]string) // If not assign here then it gives runtime error "panic: assignment to entry in nil map"到下面应该使您的代码工作:=_, ok := retMap[key]if !ok {&nbsp; &nbsp; retMap[key] = make(map[string]string)}我只是检查为“键”存储的值是否存在,如果不存在则初始化一个新映射。更新:我还重写了您的代码,以使用 Go 提供的一些内置函数,如strings.Split或strings.Trim*函数。它可能会有所帮助。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "strconv"&nbsp; &nbsp; "strings")const str = "Hello @John martin #sosos &Hi @William "func main() {&nbsp; &nbsp; retMap := make(map[string]map[string]string)&nbsp; &nbsp; retMap["group"] = make(map[string]string)&nbsp; &nbsp; retMap["user"] = make(map[string]string)&nbsp; &nbsp; retMap["hashTag"] = make(map[string]string)&nbsp; &nbsp; list := strings.Split(strings.TrimSpace(str), " ")&nbsp; &nbsp; for _, value := range list {&nbsp; &nbsp; &nbsp; &nbsp; firstCharacter := string(value[0])&nbsp; &nbsp; &nbsp; &nbsp; if firstCharacter == "@" {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retMap["user"][strconv.Itoa(len(retMap["user"])+1)] = strings.TrimLeft(value, "@")&nbsp; &nbsp; &nbsp; &nbsp; } else if firstCharacter == "&" {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retMap["group"][strconv.Itoa(len(retMap["group"])+1)] = strings.TrimLeft(value, "&")&nbsp; &nbsp; &nbsp; &nbsp; } else if firstCharacter == "#" {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retMap["hashTag"][strconv.Itoa(len(retMap["hashTag"])+1)] = strings.TrimLeft(value, "#")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println("Final String ", retMap)}
随时随地看视频慕课网APP

相关分类

Go
我要回答