Assigning value in Golang

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

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.

回答1:

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}


回答2:

func main() {      rowsList := []TabRow{         TabRow{             ColName:     "col1",             ColValue:    "col1v",             ColDataType: "string",         },         TabRow{             ColName:     "col2",             ColValue:    "col2v",             ColDataType: "int",         }}      rows := Rows(&rowsList)      resp := ResponseData{         DataType:       "json",         Component:      "backend",         ParameterName:  "test",         ParameterValue: "cases",         TableValue:     &rows,     }      fmt.Printf("%v", resp) }


回答3:

You could simplify your structure thus:

type ResponseData struct {     DataType       string           Component      string           ParameterName  string           ParameterValue string           TableValue     []*TabRow  } type TabRow struct {     ColName     string      ColValue    string      ColDataType string  }

You could then populate it with:

resp := ResponseData {     DataType: "",     Component: "",     ParameterName: "",     ParameterValue: "",     TableValue: []*TabRow{       &TabRow{"","",""},       &TabRow{"","",""},       &TabRow{"","",""},     }, }

And add a new TabRow with:

resp.TableValue = append(resp.TableValue, &TabRow{"","",""})


转载请标明出处:Assigning value in Golang
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!