英文:
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())
答案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 (
"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)
}
}
Output:
prod1
prod2
prod3
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论