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

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

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

问题

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

package main

type Item struct {
  Key string
  Value string
}

type Blah struct {
  Values []Item
}

func main() {
  var list = [...]Item {
    Item {
      Key : "Hello1",
      Value : "World1",
    },
    Item {
      Key : "Hello1",
      Value : "World1",
    },
  }

  _ = Blah {
    Values : &list,
  }
}

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

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

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

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

struct Item {
  char *key;
  char *value;
};

struct Blah {
   struct Item *values;
};

在Go语言中该怎么做呢?

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

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

英文:

I would have expected this code to work:

package main

type Item struct {
  Key string
  Value string
}

type Blah struct {
  Values []Item
}

func main() {
  var list = [...]Item {
    Item {
      Key : "Hello1",
      Value : "World1",
    },
    Item {
      Key : "Hello1",
      Value : "World1",
    },
  }

  _ = Blah {
    Values : &list,
  }
}

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:

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

In C, you'd write:

struct Item {
  char *key;
  char *value;
};

struct Blah {
   struct Item *values;
};

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中获取一个切片,你可以这样做:

_ = Blah {
    Values : list[:],
}
英文:

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:

_ = Blah {
    Values : list[:],
}

答案2

得分: 3

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

package main

type Item struct {
        Key, Value string
}

type Blah struct {
        Values []Item
}

func main() {
        list := []Item{
                {"Hello1", "World1"},
                {"Hello2", "World2"},
        }

        _ = Blah{list[:]}
}

(也可以在这里找到)

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

英文:

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

package main

type Item struct {
        Key, Value string
}

type Blah struct {
        Values []Item
}

func main() {
        list := []Item{
                {"Hello1", "World1"},
                {"Hello2", "World2"},
        }

        _ = Blah{list[:]}
}

(Also here)

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

答案3

得分: 2

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

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

package main

type Item struct {
    Key   string
    Value string
}

type Blah struct {
    Values []Item
}

func main() {
    var list = []Item{
        Item{
            Key:   "Hello1",
            Value: "World1",
        },
        Item{
            Key:   "Hello1",
            Value: "World1",
        },
    }

    _ = Blah{
        Values: list,
    }
}
英文:

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.

package main

type Item struct {
	Key   string
	Value string
}

type Blah struct {
	Values []Item
}

func main() {
	var list = []Item{
		Item{
			Key:   "Hello1",
			Value: "World1",
		},
		Item{
			Key:   "Hello1",
			Value: "World1",
		},
	}

	_ = Blah{
		Values: list,
	}
}

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:

确定