How to create array of objects in golang?

huangapple go评论130阅读模式
英文:

How to create array of objects in golang?

问题

我有一个需求,需要将对象数组存储在一个变量中。这些对象的类型各不相同。请参考以下示例:

  1. v := [ {"name":"ravi"},
  2. ["art","coding","music","travel"],
  3. {"language":"golang"},
  4. {"experience":"no"}
  5. ]

请注意,第二个元素本身是一个字符串数组。经过研究,我考虑将其存储为接口类型,如下所示:

  1. var v interface{} = [ {"name":"ravi"},
  2. ["art","coding","music","travel"],
  3. {"language":"golang"},
  4. {"experience":"no"}
  5. ]

然而,我遇到了一些编译错误,我找不出原因。

英文:

I have a requirement in which I need to store array of objects in a variable. The objects are of different types. Refer to following example:

  1. v := [ {"name":"ravi"},
  2. ["art","coding","music","travel"],
  3. {"language":"golang"},
  4. {"experience":"no"}
  5. ]

Notice the second element is array of string itself. After research, I thought of storing this as interface type like:

  1. var v interface{} = [ {"name":"ravi"},
  2. ["art","coding","music","travel"],
  3. {"language":"golang"},
  4. {"experience":"no"}
  5. ]

Still, I am getting few compilation errors which I am not able to find out.

答案1

得分: 34

你要求的是可能的 - playground链接

  1. package main
  2. import "fmt"
  3. func main() {
  4. v := []interface{}{
  5. map[string]string{"name": "ravi"},
  6. []string{"art", "coding", "music", "travel"},
  7. map[string]string{"language": "golang"},
  8. map[string]string{"experience": "no"},
  9. }
  10. fmt.Println(v)
  11. }

但你可能不想这样做。你正在与类型系统作斗争,我会质疑为什么你要这样使用Go。考虑利用类型系统 - playground链接

  1. package main
  2. import "fmt"
  3. type candidate struct {
  4. name string
  5. interests []string
  6. language string
  7. experience bool
  8. }
  9. func main() {
  10. candidates := []candidate{
  11. {
  12. name: "ravi",
  13. interests: []string{"art", "coding", "music", "travel"},
  14. language: "golang",
  15. experience: false,
  16. },
  17. }
  18. fmt.Println(candidates)
  19. }
英文:

What you're asking for is possible -- playground link:

  1. package main
  2. import "fmt"
  3. func main() {
  4. v := []interface{}{
  5. map[string]string{"name": "ravi"},
  6. []string{"art", "coding", "music", "travel"},
  7. map[string]string{"language": "golang"},
  8. map[string]string{"experience": "no"},
  9. }
  10. fmt.Println(v)
  11. }

But you probably don't want to be doing this. You're fighting the type system, I would question why you're using Go if you were doing it like this. Consider leveraging the type system -- playground link:

  1. package main
  2. import "fmt"
  3. type candidate struct {
  4. name string
  5. interests []string
  6. language string
  7. experience bool
  8. }
  9. func main() {
  10. candidates := []candidate{
  11. {
  12. name: "ravi",
  13. interests: []string{"art", "coding", "music", "travel"},
  14. language: "golang",
  15. experience: false,
  16. },
  17. }
  18. fmt.Println(candidates)
  19. }

答案2

得分: -5

完美的Python语法,但不幸的是Go使用了一些不太易读的东西。
https://golang.org/ref/spec#Composite_literals

英文:

Perfect python syntax, but go unfortunately use something less readable.
https://golang.org/ref/spec#Composite_literals

huangapple
  • 本文由 发表于 2015年10月9日 14:29:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/33031354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定