The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
hcyg

Go map[int]转[]string 的问题 很神奇

  •  
  •   hcyg · Feb 8, 2020 · 4209 views
    This topic created in 2310 days ago, the information mentioned may be changed or developed.
    if data,ok := data.(map[int]string);ok {
    	s := make([]string,len(data))
    	count := 0
    	fmt.Println("this is map[int][]string:-->",data)
    	for _,v := range data {
    		fmt.Println("this is v:-->",v)
    		s[count] = v
    		fmt.Println("this is s:-->",s)
    	}
    }
    

    这是一部分的代码 主要是将 map 转成 string 数组,但是结果如下:

    this is map[int][]string:--> map[2:32||159_tea222r||0||0 9:32||159_tea2222332r||0||0 19:32||159_tea22345762r||0||0]
    
    this is v:--> 32||159_tea22345762r||0||0
    this is s:--> [32||159_tea22345762r||0||0  ]
    this is v:--> 32||159_tea2222332r||0||0
    this is s:--> [32||159_tea2222332r||0||0  ]
    this is v:--> 32||159_tea222r||0||0
    this is s:--> [32||159_tea222r||0||0  ]
    

    赋值不了给 s 只有一个可以赋值

    如果我是连接成一个字符串就可以 这个 data 是我经过对比 delete 之后得出来的

    Supplement 1  ·  Feb 8, 2020
    count++我代码里面是有的 这里我忘记加上去了
    Supplement 2  ·  Feb 8, 2020
    debug 的时候 map 里面只有一个键有值 其他的都都是 key -> ,打印的时候又是全部都有
    11 replies    2020-02-09 12:17:42 +08:00
    znood
        1
    znood  
       Feb 8, 2020   ❤️ 1
    楼主不妨每次用完 count 给人家 +1 个鸡腿呢😉
    mocos
        2
    mocos  
       Feb 8, 2020
    for-range 时 v 是复制切片的的值,然而 v 的指针地址是没变的。所以迭代完成的时候,因为读取的 v 的指针,v 的地址被写入,装入的值也是最后迭代的值
    xsephiroth
        3
    xsephiroth  
       Feb 8, 2020 via iPhone
    能取个 idx 或者 count++吗,自己的代码稍微看看吧
    stevenbipt
        4
    stevenbipt  
       Feb 8, 2020
    count 值一直没变吧???
    SingeeKing
        5
    SingeeKing  
    PRO
       Feb 8, 2020
    if data,ok := data.(map[int]string);ok {
    s := make([]string,len(data))
    fmt.Println("this is map[int][]string:-->",data)
    for i,v := range data {
    fmt.Println("this is v:-->",v)
    s[i] = v
    fmt.Println("this is s:-->",s)
    }
    }
    fantastM
        6
    fantastM  
       Feb 8, 2020   ❤️ 1
    count++ 正解,不过也可以这样
    ```go
    data := map[int]string{
    1: "one",
    2: "tow",
    3: "three",
    }

    var s []string
    for _, val := range data {
    s = append(s, val)
    }
    ```
    gamexg
        7
    gamexg  
       Feb 8, 2020
    我习惯这样:

    ``` go

    if data,ok := data.(map[int]string);ok {
    s := make([]string,0,len(data))
    for _,v := range data {
    s=append(s,v)
    }
    }

    ```
    hcyg
        8
    hcyg  
    OP
       Feb 8, 2020
    count++ 我漏加上去 我代码里面是又 count++的
    petelin
        9
    petelin  
       Feb 8, 2020 via iPhone
    这是 string
    index90
        10
    index90  
       Feb 9, 2020 via iPhone
    Make 的时候你只设了容量,没有设长度
    ikw
        11
    ikw  
       Feb 9, 2020 via iPhone
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2836 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 05:58 · PVG 13:58 · LAX 22:58 · JFK 01:58
    ♥ Do have faith in what you're doing.