[英]how to get a slice of a multidimensional array using an array with index keys
[英]How to make a Multidimensional Array or Slice using Reflection
我目前正在从事一个项目,该项目将在Go语言中为使用多维数组的另一种语言创建绑定。 我正在尝试找到一种动态方法来任意创建具有多个维度的切片切片或数组阵列。 目前,我正在查看反射文档,但似乎没有什么比这更直观,这似乎是我需要做的一种直观方法。
对于上下文,我可以获取类型信息并将其解析为以下结构:
const (
IntTy byte = iota
UintTy
BoolTy
StringTy
AddressTy
HashTy
FixedPointTy
FunctionTy
FixedBytesTy
StaticArrayTy
DynamicArrayTy
MultiDimensionalArrayTy
StructTy
BytesTy
)
// Type is the reflection of the supported argument type
type Type struct {
// Slice descriptions
IsStatic bool // Determines if its a static array
IsDynamic bool // Determines if its a dynamic array
IsMultiDimensional bool // Determine if there is more than dimension to the array
SliceSize int // Size of the slice if it's static
Dimensions int // Number of dimensions if type is a multidimensional array
// If applicable (struct, slice), the underlying type
Elem *Type
Kind reflect.Kind // corresponding go Kind.
Type reflect.Type // corresponding go Type.
Size int // type size (denotes uint256, uint248, etc.)
T byte // Our own type checking
RequiresOffset bool // denotes whether the type needs an offset
stringKind string // holds the unparsed string for deriving signatures
}
请注意,有一些字段可以帮助描述切片和基础类型(我们希望尽可能准确)。 我有一个类型字节以及表示该切片的元素数量和尺寸的元素。 但是,从这些描述中创建golang多维切片类型的直观方法是什么? 展示此算法的一种算法,例如[] [] uint或[] [] [] uint,将不胜感激。
好的,我想我第一次看的并不足够,但是回想起来,答案是显而易见的。 我在这里使用该Go Playground做了一个示例: https : //play.golang.org/p/J0pAGNIpDX
ICYMI,基本方法是获取您正在使用的类型的类型(基本类型),然后在该类型中循环运行SliceOf / ArrayOf函数以获取所需的任意尺寸。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.