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

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

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

问题

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

package main

import (
    "fmt"
    "reflect"
)

type My struct{
    Name string
    Id int
}

func main() {
    my := &My{}
    myType := reflect.TypeOf(my)
    fmt.Println(myType)
    //v := reflect.New(myType).Elem().Interface()

    // 我想用My创建一个数组
    //a := make([](myType.(type),0)  //无法编译通过
    //a := make([]v.(type),0)  ////无法编译通过
    fmt.Println(a)
}

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

英文:

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

import (
    "fmt"
    "reflect"
)

type My struct{
    Name string
    Id int
}

func main() {
    my := &My{}
    myType := reflect.TypeOf(my)
    fmt.Println(myType)
    //v := reflect.New(myType).Elem().Interface()
	
    // I want to make array  with My
    //a := make([](myType.(type),0)  //can compile
    //a := make([]v.(type),0)  ////can compile
    fmt.Println(a)
}

答案1

得分: 9

我相信这就是你要找的:

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

工作示例:

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

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

I believe this is what you're looking for:

 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:

 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 使用,并且必须保持同步

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

at1 := ArrayOf(5, TypeOf(string("")))
at := ArrayOf(6, at1)
v1 := New(at).Elem()
v2 := New(at).Elem()

v1.Index(0).Index(0).Set(ValueOf("abc"))
v2.Index(0).Index(0).Set(ValueOf("efg"))
if i1, i2 := v1.Interface(), v2.Interface(); i1 == i2 {
	t.Errorf("constructed arrays %v and %v should not be equal", i1, i2)
}
英文:

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:

at1 := ArrayOf(5, TypeOf(string("")))
at := ArrayOf(6, at1)
v1 := New(at).Elem()
v2 := New(at).Elem()

v1.Index(0).Index(0).Set(ValueOf("abc"))
v2.Index(0).Index(0).Set(ValueOf("efg"))
if i1, i2 := v1.Interface(), v2.Interface(); i1 == i2 {
	t.Errorf("constructed arrays %v and %v should not be equal", i1, i2)
}

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:

确定