dongyan5641 2017-01-31 09:43
浏览 61

如何使用go在没有索引名称的情况下将值存储在哈希图中?

I want to store some values in hashmap without index name. I mean derived from the array and hashmap.

Example :

{"name":"attn",1,5,6,7,8}

Output of variable (just for demonstration):

( "name" : "attn", 0:1, 1:5, 2:6, 3:7, 4:8, )

Or another example:

{ 0:"start","name":"mattn","age":39,"child":[1,2,3,4,5,9:1] }

How can do this in Go?

Maybe I need new data type? :)

Please, help me! Thank you!

  • 写回答

2条回答 默认 最新

  • douhuireng4407 2017-01-31 10:08
    关注

    You can do something like this:

    package main
    
    import "fmt"
    
    func main() {
        dynMap := map[interface{}]interface{}{
            "name": "attn",
            0:      1,
            1:      5,
            2:      6,
            3:      7,
            4:      8,
        }
        fmt.Printf("%v
    ", dynMap)
    
        dynMap2 := map[interface{}]interface{}{
            0:       "start",
            "name":  "mattn",
            "age":   39,
            "child": []int{1, 2, 3, 4, 5, 9:1}, 
    
        }
        fmt.Printf("%v
    ", dynMap2)
    }
    

    Except for "child":[1,2,3,4,5,9:1] - I fail to understand what you really want for [9:1] Thanks for correction: this is also doable.

    And, although I like python too, go is not python (or js/ruby/etc). If you will do a lot interface{} and reflect on it - it's possible but it's a pain.

    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭