我需要这样做:在go语言中,请帮助,谢谢
var size_IO= map[string]int{"AI":32,"DI":64,"AO":32}
type IO_point struct{
Name string
}
type AI struct {
CART [64]IO_point
Type string
IO_points [size_IO[Type]]IO_point
}发布于 2022-08-28 19:39:06
简短回答:,你不能这么做。
数组的长度是类型声明的一部分,它的值必须在编译时解析。所以它应该是恒定的。
const arr_size= 5
type IO_point struct {
Name string
}
type AI struct {
CART [64]IO_point
Type string
IO_points [arr_size]IO_point
}在您的情况下,您需要一个片,它是一个动态数据结构,比数组更灵活。这里就是一个例子:
var size_IO = map[string]int{"AI": 32, "DI": 64, "AO": 32}
type IO_point struct {
Name string
}
type AI struct {
CART [64]IO_point
Type string
IO_points []IO_point
}
func NewAi(cart [64]IO_point, t string) AI {
return AI{
CART: cart,
Type: t,
IO_points: make([]IO_point, 0, size_IO[t]),
}
}https://stackoverflow.com/questions/73521535
复制相似问题