how to make array with given name(string) in golang with reflect

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

how to make array with given name(string) in golang with reflect

问题

我想在golang中创建一个带有名称的数组,但是我遇到了一些错误。以下是我的代码:

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. type My struct{
  7. Name string
  8. Id int
  9. }
  10. func main() {
  11. my := &My{}
  12. myType := reflect.TypeOf(my)
  13. fmt.Println(myType)
  14. //v := reflect.New(myType).Elem().Interface()
  15. // 我想用My创建一个数组
  16. //a := make([](myType.(type),0) //无法编译通过
  17. //a := make([]v.(type),0) ////无法编译通过
  18. fmt.Println(a)
  19. }

请注意,我只会翻译代码部分,不会回答关于翻译的问题。

英文:

I want to make array with name in golang, but I got some error
here is my code
package main

  1. import (
  2. "fmt"
  3. "reflect"
  4. )
  5. type My struct{
  6. Name string
  7. Id int
  8. }
  9. func main() {
  10. my := &My{}
  11. myType := reflect.TypeOf(my)
  12. fmt.Println(myType)
  13. //v := reflect.New(myType).Elem().Interface()
  14. // I want to make array with My
  15. //a := make([](myType.(type),0) //can compile
  16. //a := make([]v.(type),0) ////can compile
  17. fmt.Println(a)
  18. }

答案1

得分: 9

我相信这就是你要找的:

  1. slice := reflect.MakeSlice(reflect.SliceOf(myType), 0, 0).Interface()

工作示例:

另外,大多数情况下,一个空切片比容量为零的切片更合适。如果你想要一个空切片,可以使用以下代码:

  1. slice := reflect.Zero(reflect.SliceOf(myType)).Interface()
英文:

I believe this is what you're looking for:

  1. slice := reflect.MakeSlice(reflect.SliceOf(myType), 0, 0).Interface()

Working example:

As a side note, in most cases a nil slice is more suitable than one with capacity zero. If you want a nil slice, this would do instead:

  1. slice := reflect.Zero(reflect.SliceOf(myType)).Interface()

答案2

得分: 5

注意:如果您想创建一个实际的数组(而不是切片),您可以在Go 1.5(2015年8月)中使用**reflect.ArrayOf**。

请参见评审 4111提交 918fdae,由Sebastien Binet (sbinet)修复了2013年问题 5996

> ## reflect: 实现 ArrayOf

> 此更改公开了reflect.ArrayOf,以在运行时创建新的reflect.Type数组类型,当给定一个reflect.Type元素时。

> - reflect: 实现 ArrayOf

  • reflect: 为 ArrayOf 添加测试
  • runtime: 文档化 typeAlg 被 reflect 使用,并且必须保持同步

这允许进行类似于以下的测试:

  1. at1 := ArrayOf(5, TypeOf(string("")))
  2. at := ArrayOf(6, at1)
  3. v1 := New(at).Elem()
  4. v2 := New(at).Elem()
  5. v1.Index(0).Index(0).Set(ValueOf("abc"))
  6. v2.Index(0).Index(0).Set(ValueOf("efg"))
  7. if i1, i2 := v1.Interface(), v2.Interface(); i1 == i2 {
  8. t.Errorf("constructed arrays %v and %v should not be equal", i1, i2)
  9. }
英文:

Note: if you want to create an actual array (and not a slice), you will have in Go 1.5 (August 2015) reflect.ArrayOf.

See review 4111 and commit 918fdae by Sebastien Binet (sbinet), fixing a 2013 issue 5996.

> ## reflect: implement ArrayOf

> This change exposes reflect.ArrayOf to create new reflect.Type array
types at runtime, when given a reflect.Type element.

> - reflect: implement ArrayOf

  • reflect: tests for ArrayOf
  • runtime: document that typeAlg is used by reflect and must be kept in
    synchronized

That allows for test like:

  1. at1 := ArrayOf(5, TypeOf(string("")))
  2. at := ArrayOf(6, at1)
  3. v1 := New(at).Elem()
  4. v2 := New(at).Elem()
  5. v1.Index(0).Index(0).Set(ValueOf("abc"))
  6. v2.Index(0).Index(0).Set(ValueOf("efg"))
  7. if i1, i2 := v1.Interface(), v2.Interface(); i1 == i2 {
  8. t.Errorf("constructed arrays %v and %v should not be equal", i1, i2)
  9. }

huangapple
  • 本文由 发表于 2013年9月4日 10:05:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/18604345.html
匿名

发表评论

匿名网友

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

确定