在接口内部访问结构体的值

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

Access struct values inside an interface

问题

我有一个类似的interface{},如下所示:

Rows interface{}

Rows接口中,我放置了ProductResponse结构体。

type ProductResponse struct {
    CompanyName     string                        `json:"company_name"`
    CompanyID       uint                          `json:"company_id"`
    CompanyProducts []*Products                   `json:"CompanyProducts"`
}
type Products struct {
    Product_ID          uint      `json:"id"`
    Product_Name        string    `json:"product_name"`
}

我想要访问Product_Name的值。如何访问呢?
我可以通过使用reflect包来访问外部的值(CompanyName,CompanyID)。

value := reflect.ValueOf(response)
CompanyName := value.FieldByName("CompanyName").Interface().(string)

我无法访问Products结构体的值。如何做到这一点?

英文:

I have an interface{} that is similar like -

Rows interface{}

In the Rows interface i put ProductResponse struct.

type ProductResponse struct {
    CompanyName     string                        `json:"company_name"`
    CompanyID       uint                          `json:"company_id"`
    CompanyProducts []*Products                   `json:"CompanyProducts"`
}
type Products struct {
    Product_ID          uint      `json:"id"`
    Product_Name        string    `json:"product_name"`
}

I want to access Product_Name value. How to access this.
I can access outside values (CompanyName , CompanyID) by using "reflect" pkg.

value := reflect.ValueOf(response)
CompanyName := value.FieldByName("CompanyName").Interface().(string)

I am not able to access Products struct values. How to do that?

答案1

得分: 7

你可以使用type assertion

pr := rows.(ProductResponse)
fmt.Println(pr.CompanyProducts[0].Product_ID)
fmt.Println(pr.CompanyProducts[0].Product_Name)

或者你可以使用reflect包:

rv := reflect.ValueOf(rows)

// 获取CompanyProducts字段的值
v := rv.FieldByName("CompanyProducts")
// 该值是一个切片,所以使用.Index(N)获取该切片中的第N个元素
v = v.Index(0)
// 元素的类型是*Product,所以使用.Elem()取消引用指针并获取结构体值
v = v.Elem()

fmt.Println(v.FieldByName("Product_ID").Interface())
fmt.Println(v.FieldByName("Product_Name").Interface())

https://play.golang.org/p/RAcCwj843nM

英文:

You can use type assertion:

pr := rows.(ProductResponse)
fmt.Println(pr.CompanyProducts[0].Product_ID)
fmt.Println(pr.CompanyProducts[0].Product_Name)

Or you can use the reflect package:

rv := reflect.ValueOf(rows)

// get the value of the CompanyProducts field
v := rv.FieldByName("CompanyProducts")
// that value is a slice, so use .Index(N) to get the Nth element in that slice
v = v.Index(0)
// the elements are of type *Product so use .Elem() to dereference the pointer and get the struct value
v = v.Elem()

fmt.Println(v.FieldByName("Product_ID").Interface())
fmt.Println(v.FieldByName("Product_Name").Interface())

https://play.golang.org/p/RAcCwj843nM

答案2

得分: 3

不要使用反射,而应该使用类型断言。

res, ok := response.(ProductResponse) 
if ok { // 成功
   res.CompanyProducts[0].Product_Name // 访问 Product_Name 或 Product_ID
} else {
   // 处理类型断言失败
}
英文:

Instead of using reflection you should use type assertion.

res, ok := response.(ProductResponse) 
if ok { // Successful
   res.CompanyProducts[0].Product_Name // Access Product_Name or Product_ID
} else {
   // Handle type assertion failure 
}

答案3

得分: 1

你可以通过使用for循环迭代CompanyProducts切片来访问Product_Name的值,而无需使用"reflect"包。我为你的情景创建了一个简单的程序,如下所示:

package main

import (
	"fmt"
)

type ProductResponse struct {
	CompanyName     string      `json:"company_name"`
	CompanyID       uint        `json:"company_id"`
	CompanyProducts []*Products `json:"CompanyProducts"`
}
type Products struct {
	Product_ID   uint   `json:"id"`
	Product_Name string `json:"product_name"`
}

func main() {

	var rows2 interface{} = ProductResponse{CompanyName: "Zensar", CompanyID: 1001, CompanyProducts: []*Products{{1, "prod1"}, {2, "prod2"}, {3, "prod3"}}}

	for i := 0; i < len(rows2.(ProductResponse).CompanyProducts); i++ {
		fmt.Println(rows2.(ProductResponse).CompanyProducts[i].Product_Name)
	}

}

输出:

prod1
prod2
prod3
英文:

You can access Product_Name value without even using "reflect" pkg by simply iterating over the CompanyProducts slice by using for loop.I have created a simple program for you scenario as follows:

package main

import (
	&quot;fmt&quot;
)

type ProductResponse struct {
	CompanyName     string      `json:&quot;company_name&quot;`
	CompanyID       uint        `json:&quot;company_id&quot;`
	CompanyProducts []*Products `json:&quot;CompanyProducts&quot;`
}
type Products struct {
	Product_ID   uint   `json:&quot;id&quot;`
	Product_Name string `json:&quot;product_name&quot;`
}

func main() {

	var rows2 interface{} = ProductResponse{CompanyName: &quot;Zensar&quot;, CompanyID: 1001, CompanyProducts: []*Products{{1, &quot;prod1&quot;}, {2, &quot;prod2&quot;}, {3, &quot;prod3&quot;}}}

	for i := 0; i &lt; len(rows2.(ProductResponse).CompanyProducts); i++ {
		fmt.Println(rows2.(ProductResponse).CompanyProducts[i].Product_Name)
	}

}

Output:

prod1
prod2
prod3

huangapple
  • 本文由 发表于 2021年7月8日 18:53:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/68300260.html
匿名

发表评论

匿名网友

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

确定