Making Array of Hashes in Golang

2019-08-07 14:58发布

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'}} 
    ...,
    ... ]

2条回答
淡お忘
2楼-- · 2019-08-07 15:27

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"}},
}
查看更多
劳资没心,怎么记你
3楼-- · 2019-08-07 15:28

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.

查看更多
登录 后发表回答