在Golang中分配值

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.

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)
}

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}

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{"","",""})