你在Go语言中如何创建一个包含指向数组的指针的结构体?

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

How do you have a struct with a pointer to an array in it in Go?

问题

我本来以为这段代码会工作的:

  1. package main
  2. type Item struct {
  3. Key string
  4. Value string
  5. }
  6. type Blah struct {
  7. Values []Item
  8. }
  9. func main() {
  10. var list = [...]Item {
  11. Item {
  12. Key : "Hello1",
  13. Value : "World1",
  14. },
  15. Item {
  16. Key : "Hello1",
  17. Value : "World1",
  18. },
  19. }
  20. _ = Blah {
  21. Values : &list,
  22. }
  23. }

我以为这是正确的做法;Values是一个切片,list是一个数组。&list应该是一个切片,可以赋值给Item[],对吗?

...但是实际上,它报错了,错误信息是:

  1. cannot use &list (type *[2]Item) as type []Item in assignment

在C语言中,你会这样写:

  1. struct Item {
  2. char *key;
  3. char *value;
  4. };
  5. struct Blah {
  6. struct Item *values;
  7. };

在Go语言中该怎么做呢?

我看到了这个问题:
https://stackoverflow.com/questions/2439453/go-using-a-pointer-to-array

...但是要么答案是针对Go的旧版本,要么就是完全错误的。:/

英文:

I would have expected this code to work:

  1. package main
  2. type Item struct {
  3. Key string
  4. Value string
  5. }
  6. type Blah struct {
  7. Values []Item
  8. }
  9. func main() {
  10. var list = [...]Item {
  11. Item {
  12. Key : "Hello1",
  13. Value : "World1",
  14. },
  15. Item {
  16. Key : "Hello1",
  17. Value : "World1",
  18. },
  19. }
  20. _ = Blah {
  21. Values : &list,
  22. }
  23. }

I thought this would be the correct way of doing this; Values is a slice, list is an array. &list should be a slice, which is assignable to Item[], right?

...but instead, it errors with the message:

  1. cannot use &list (type *[2]Item) as type []Item in assignment

In C, you'd write:

  1. struct Item {
  2. char *key;
  3. char *value;
  4. };
  5. struct Blah {
  6. struct Item *values;
  7. };

How do you do that in Go?

I saw this question:
https://stackoverflow.com/questions/2439453/go-using-a-pointer-to-array

...but either the answers are for a previous version of Go, or they're just plain wrong. :/

答案1

得分: 4

一个切片不仅仅是一个指向数组的指针,它还有一个内部表示,其中包含它的长度和容量。

如果你想从list中获取一个切片,你可以这样做:

  1. _ = Blah {
  2. Values : list[:],
  3. }
英文:

A slice is not simply a pointer to an array, it has an internal representation which contains its length and capacity.

If you want to get a slice from list you can do:

  1. _ = Blah {
  2. Values : list[:],
  3. }

答案2

得分: 3

Go是一种幸运的语言,它并不像OP所看起来的那样冗长。这段代码可以工作:

  1. package main
  2. type Item struct {
  3. Key, Value string
  4. }
  5. type Blah struct {
  6. Values []Item
  7. }
  8. func main() {
  9. list := []Item{
  10. {"Hello1", "World1"},
  11. {"Hello2", "World2"},
  12. }
  13. _ = Blah{list[:]}
  14. }

(也可以在这里找到)

PS:我建议不要在Go中编写C代码。

英文:

Go is, fortunately, not so verbose as it might seem from the OP. This works:

  1. package main
  2. type Item struct {
  3. Key, Value string
  4. }
  5. type Blah struct {
  6. Values []Item
  7. }
  8. func main() {
  9. list := []Item{
  10. {"Hello1", "World1"},
  11. {"Hello2", "World2"},
  12. }
  13. _ = Blah{list[:]}
  14. }

(Also here)

PS: Let me suggest to not write C in Go.

答案3

得分: 2

当你开始使用Go时,我建议完全忽略数组,只使用切片。数组很少使用,并且会给Go初学者带来很多麻烦。如果你有一个切片,你就不需要一个指向它的指针,因为它是一个引用类型。

这是你的示例,使用切片而没有指针,更符合惯用方式。

  1. package main
  2. type Item struct {
  3. Key string
  4. Value string
  5. }
  6. type Blah struct {
  7. Values []Item
  8. }
  9. func main() {
  10. var list = []Item{
  11. Item{
  12. Key: "Hello1",
  13. Value: "World1",
  14. },
  15. Item{
  16. Key: "Hello1",
  17. Value: "World1",
  18. },
  19. }
  20. _ = Blah{
  21. Values: list,
  22. }
  23. }
英文:

When you are starting out with Go ignore arrays completely and just use slices is my advice. Arrays are rarely used and cause Go beginners a lot of trouble. If you have a slice then you don't need a pointer to it since it is a reference type.

Here is your example with a slice and no pointers which is much more idiomatic.

  1. package main
  2. type Item struct {
  3. Key string
  4. Value string
  5. }
  6. type Blah struct {
  7. Values []Item
  8. }
  9. func main() {
  10. var list = []Item{
  11. Item{
  12. Key: "Hello1",
  13. Value: "World1",
  14. },
  15. Item{
  16. Key: "Hello1",
  17. Value: "World1",
  18. },
  19. }
  20. _ = Blah{
  21. Values: list,
  22. }
  23. }

huangapple
  • 本文由 发表于 2013年2月11日 12:24:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/14805861.html
匿名

发表评论

匿名网友

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

确定