duandao3265
2017-04-12 21:15
浏览 167
已采纳

在Golang中制作哈希数组

I'm brand new to Go and having some trouble with nested data structures. Below is a array of hashes I mocked up that I need to make in Golang. I'm just confused with the whole having to declare the variable type beforehand and whatnot. Any ideas?

 var Array = [
   {name: 'Tom', dates: [20170522, 20170622], images: {profile: 'assets/tom-profile', full: 'assets/tom-full'}},
   {name: 'Pat', dates: [20170515, 20170520], images: {profile: 'assets/pat-profile', full: 'assets/pat-full'}} 
    ...,
    ... ]

图片转代码服务由CSDN问答提供 功能建议

我是Go语言的新手,并且在嵌套数据结构方面遇到了一些麻烦。 下面是我需要在Golang中制作的一系列哈希值。 我只是对整个必须事先声明变量类型而感到困惑。 有任何想法吗?

  var Array = [
 {name:'Tom',date:[20170522,20170622],images:{profile:'assets / tom-profile',full:  'assets / tom-full'}},
 {名称:'Pat',日期:[20170515,20170520],图像:{profile:'assets / pat-profile',完整:'assets / pat-full'}  } 
 ...,
 ...] 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douba05167 2017-04-12 21:38
    已采纳

    What is called a 'hash' in Ruby is called a 'map' (translating keys to values) in Go.

    However, Go is a statically typechecked language. A map can only map a certain type to another type, e.g. a map[string]int maps string values to integeger. That is not what you want here.

    So what you want is a struct. Indeed, you need to define the type beforehand. So what you would do:

    // declaring a separate 'Date' type that you may or may not want to encode as int. 
    type Date int 
    type User struct {
        Name string
        Dates []Date
        Images map[string]string
    }
    

    Now that this type is defined, you can use it in another type:

    ar := []User{
      User{
        Name: "Tom",
        Dates: []Date{20170522, 20170622},
        Images: map[string]string{"profile":"assets/tom-profile", "full": "assets/tom-full"},
      },
      User{
        Name: "Pat",
        Dates: []Date{20170515, 20170520},
        Images: map[string]string{"profile":"assets/pat-profile", "full": "assets/pat-full"},
      },
    }   
    

    Note how we are defining User as a struct, but images as a map of string to image. You could also define a separate Image type:

    type Image struct {
      Type string // e.g. "profile"
      Path string // e.g. "assets/tom-profile"
    }
    

    You would then not define Images as map[string]string but as []Image, that is, slice of Image structs. Which one is more appropriate depends on the use case.

    已采纳该答案
    打赏 评论
  • doutuo7156 2017-04-12 21:49

    You don't need to declare the variable type beforehand, at least not in this simple example, although you need to "mention" your types when initializing your values with composite literals.

    For example this [{}] (array of objects?) makes no sense to the Go compiler, instead you need to write something like this []map[string]interface{}{} (slice of maps whose keys are strings and whose values can have any type)

    To break it down:

    1. [] - slice of whatever type comes after it
    2. map - builtin map (think hash)
    3. [string] - inside the square brackets is the type of the map key, can be almost any type
    4. interface{} - the type of the map values
    5. {} - this initializes/allocate the whole thing

    So your example in Go would look something like this:

    var Array = []map[string]interface{}{
        {"name":"Tom", "dates": []int{20170522, 20170622}, "images": map[string]string{"profile": "assets/tom-profile", "full": "assets/tom-full"}},
        {"name":"Pat", "dates": []int{20170515, 20170520}, "images": map[string]string{"profile": "assets/pat-profile", "full": "assets/pat-full"}},
        // ...
    }
    

    Read more on maps and what key types you can use here: https://golang.org/ref/spec#Map_types

    That said, in Go, most of the time, you would first define your structured types more concretely and then use them instead of maps, so something like this makes more sense in Go:

    type User struct {
        Name string
        Dates []int
        Images Images
    }
    
    type Images struct {
        Profile string
        Full string
    }
    
    var Array = []User{
        {Name:"Tom", Dates:[]int{20170522, 20170622}, Images:Images{Profile:"assets/tom-profile", Full:"assets/tom-full"}},
        {Name:"Pat", Dates:[]int{20170515, 20170520}, Images:Images{Profile:"assets/pat-profile", Full:"assets/pat-full"}},
    }
    
    打赏 评论

相关推荐 更多相似问题