在Golang中分配价值 [英] Assigning value in Golang
问题描述
我创建了一个类型变量
pre $ var RespData [] ResponseData
ResponseData的结构如下:
type ResponseData struct {
DataType字符串
组件字符串
ParameterName字符串
ParameterValue字符串
TableValue * []行
}
类型TabRow struct {
ColName字符串
ColValue字符串
ColDataType字符串
}
类型行* [] TabRow
我想填充 * []行
类型的 TableValue
p>
您可以通过在 TableValue
中分配任何值来告诉我一个例子。
TableValue
是指向 []行的指针
(分片行
)。 行
是一个指向 [] TabRow
(片段 TabRow
)。因此,您可以使用切片文字创建行数
/ a>,并使用&
<地址,所以你有一个指向 [] TabRow
的指针 - 这将是类型行
。
你可以通过使用另一个切片文字(创建一个 []来获得一个指向
),并将其地址类型为 []行
]行 * []行
,这是 TableValue
,所以你可以直接把它分配给
ResponseData.TableValue
。
这:
var tv1 Rows =& [] TabRow {TabRow {name11,value11,type11} ,
TabRow {name12,value12,type12}}
var tv2 Rows =& [] TabRow {TabRow {name21,value21,type21},
TabRow {name22,value22,type22}}
var Tab leValue * [] Rows =& []行{tv1,tv2}
fmt.Println(TableValue)
for _,v:= range * TableValue {
fmt。 Println(v)
}
输出:
& [0x10436180 0x10436190]
& [{name11 value11 type11} {name12 value12 type12}]
& [{name21 value21 type21} {name22 value22 type22}]
c $ c> TabRow ),你甚至可以省略这个类型,它变成这样: var tv1 Rows =& [] TabRow {{name11,value11,type11},
{name12,value12,type12}}
var tv2 Rows =& [] TabRow {{name21,value21,type21},
{name22,value22,type22}}
tv1:=& [] TabRow {{name11,value11,type11 },{name12,value12,type12}}
tv2:=& [] TabRow {{name21,value21,type21},{name22 value22,type22}}
TableValue:=& []行{tv1,tv2}
I create a var of type
var RespData []ResponseData
ResponseData is a structure as below:
type ResponseData struct {
DataType string
Component string
ParameterName string
ParameterValue string
TableValue *[]Rows
}
type TabRow struct {
ColName string
ColValue string
ColDataType string
}
type Rows *[]TabRow
I want to fill TableValue
of type *[]Rows
.
Can you please tell me with an example by assigning any values in the TableValue
.
TableValue
is a pointer to []Rows
(slice of Rows
).
Rows
is a pointer to a []TabRow
(slice of TabRow
). So you can create a Rows
value with a slice literal, and take its address with &
so you have a pointer to []TabRow
- this will be of type Rows
.
And you can obtain a pointer to []Rows
by using another slice literal (which creates a []Rows
) and take its address which will be of type *[]Rows
which is the type of TableValue
so you can directly assign this to ResponseData.TableValue
.
So you could do it like this:
var tv1 Rows = &[]TabRow{TabRow{"name11", "value11", "type11"},
TabRow{"name12", "value12", "type12"}}
var tv2 Rows = &[]TabRow{TabRow{"name21", "value21", "type21"},
TabRow{"name22", "value22", "type22"}}
var TableValue *[]Rows = &[]Rows{tv1, tv2}
fmt.Println(TableValue)
for _, v := range *TableValue {
fmt.Println(v)
}
Output:
&[0x10436180 0x10436190]
&[{name11 value11 type11} {name12 value12 type12}]
&[{name21 value21 type21} {name22 value22 type22}]
Try it on the Go Playground.
In the slice literal where you specify the elements (of type TabRow
), you can even leave out the type, and it becomes this:
var tv1 Rows = &[]TabRow{{"name11", "value11", "type11"},
{"name12", "value12", "type12"}}
var tv2 Rows = &[]TabRow{{"name21", "value21", "type21"},
{"name22", "value22", "type22"}}
And if you use Short variable declaration, you can even shorten it further (try it on Playground):
tv1 := &[]TabRow{{"name11", "value11", "type11"}, {"name12", "value12", "type12"}}
tv2 := &[]TabRow{{"name21", "value21", "type21"}, {"name22", "value22", "type22"}}
TableValue := &[]Rows{tv1, tv2}
这篇关于在Golang中分配价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!