Assigning value in Golang

huangapple go评论78阅读模式
英文:

Assigning value in Golang

问题

我创建了一个类型为var RespData []ResponseData的变量。

ResponseData是以下结构体的类型:

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

我想要填充TableValue,它的类型是*[]Rows

你能否给我一个例子,将任意值分配给TableValue

英文:

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

得分: 4

TableValue是指向[]RowsRows的切片)的指针。

Rows是指向[]TabRowTabRow的切片)的指针。因此,您可以使用slice字面量创建一个Rows值,并使用&获取其地址,从而获得指向[]TabRow的指针 - 这将是Rows类型。

您可以使用另一个切片字面量(创建一个[]Rows)并获取其地址,从而获得指向[]Rows的指针,该指针的类型是*[]Rows,这是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 TableValue *[]Rows = &[]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}]

在指定元素(类型为TabRow)的切片字面量中,甚至可以省略类型,变成这样:

var tv1 Rows = &[]TabRow{{"name11", "value11", "type11"},
        {"name12", "value12", "type12"}}
var tv2 Rows = &[]TabRow{{"name21", "value21", "type21"},
        {"name22", "value22", "type22"}}

如果使用短变量声明,甚至可以进一步缩短(在Playground上尝试):

tv1 := &[]TabRow{{"name11", "value11", "type11"}, {"name12", "value12", "type12"}}
tv2 := &[]TabRow{{"name21", "value21", "type21"}, {"name22", "value22", "type22"}}
TableValue := &[]Rows{tv1, tv2}
英文:

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

得分: 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)
}
type TabRow struct {
    ColName     string
    ColValue    string
    ColDataType string
}

type Rows []TabRow

type ResponseData struct {
    DataType       string
    Component      string
    ParameterName  string
    ParameterValue string
    TableValue     *Rows
}

以上是要翻译的内容。

英文:
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

得分: 0

你可以简化你的结构如下:

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

type TabRow struct {
    ColName     string 
    ColValue    string 
    ColDataType string 
}

然后你可以这样填充它:

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

要添加一个新的 TabRow,可以使用以下代码:

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

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

huangapple
  • 本文由 发表于 2015年3月12日 18:57:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/29007904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定