英文:
Call struct from map of structs
问题
嗨,我正在尝试类似以下示例的东西。
作为一个PHP开发者(我知道!),这让我很困扰。我已经阅读了反射定律和其他资料,但这超出了我的理解范围。我使用的方法可能是错误的...希望有人能指点我正确的方向。
具体使用的目的是,版本01、02或03是来自外部参数的,基于此,我需要获取相应的结构体并用数据库值填充它。
package V01
type Struct1 struct{
Field1 string
Field2 string
}
type Struct2 struct{
Field1 string
Field2 string
}
---
package V02
type Struct1 struct{
Field1 string
Field2 string
ExtraField1 string
}
type Struct2 struct{
Field1 string
Field2 string
ExtraField2 string
ExtraField3 string
}
---
var VStructs = map[string]map[string]interface{}{
"01": map[string]interface{}{
"Struct1": V01.Struct1{},
"Struct2": V01.Struct2{},
},
"02": map[string]interface{}{
"Struct1": V02.Struct1{},
"Struct2": V02.Struct2{},
},
"03" : map[string]interface{}{
"Struct1": V01.Struct1{},
"Struct2": V02.Struct2{},
},
}
---
// 获取结构体字段名等信息。
fmt.Printf("%+v\n", VStructs["01"]["Struct1"])
// 但是我无法访问任何字段,因为它是一个接口。
fmt.Println(VStructs["01"]["Struct1"].Field1) // 报错!
// 类型切换也不起作用,因为版本可能是可变的。
s := VStructs["01"]["Struct1"].Field1
switch x := s.(type) {
case reflect.Struct: // 报错!reflect.Struct(类型reflect.Kind)不是一个类型
fmt.Println("我是一个结构体")
default:
fmt.Println("我不是一个结构体")
}
---
所以也许你可以向我展示一种适当的方法来做这个。或者也许是一个返回正确结构体的包装函数...目前不清楚。
希望这样清楚明了,如果有需要,我可以提供更多细节。
<details>
<summary>英文:</summary>
Hi I am trying something like the following example.
My background as a PHP Developer (I know!) is giving me a hard time on this.
I've read the Laws of reflection and other sources, but this is over my head. The approach i am using is probably wrong... and hope someone can point me in the right direction.
The use of this in a concrete way is that the version 01 or 02 or 03 is comming from an external Parameter, based on that, I need to get the appropriate struct and populate it with database values.
package V01
type Struct1 struct{
Field1 string
Field2 string
}
type Struct2 struct{
Field1 string
Field2 string
}
---
package V02
type Struct1 struct{
Field1 string
Field2 string
ExtraField1 string
}
type Struct2 struct{
Field1 string
Field2 string
ExtraField2 string
ExtraField3 string
}
---
var VStructs = map[string]map[string]interface{}{
"01": map[string]interface{}{
"Struct1": V01.Struct1{},
"Struct2": V01.Struct2{},
},
"02": map[string]interface{}{
"Struct1": V02.Struct1{},
"Struct2": V02.Struct2{},
},
"03" : map[string]interface{}{
"Struct1": V01.Struct1{},
"Struct2": V02.Struct2{},
},
}
---
// I get the struct fieldnames and so on.
fmt.Printf("%+v\n", VStructs["01"]["Struct1"] )
// I cannot access any of the fields though because it is an interface
fmt.Println( VStructs["01"]["Struct1"].Field1 ) // PANIC!
// Type Switching is not working either since the version can be variable.
s := VStructs["01"]["Struct1"].Field1
switch x := s.(type) {
case reflect.Struct: // PANIC! reflect.Struct (type reflect.Kind) is not a type
fmt.Println("I am an struct")
default:
fmt.Println("I am an no struct")
}
---
So maybe can show me an appropriate way of doing this. Or maybe an wrapper function to return the right struct... no clue at this point.
Hope it is clear, and will elaborate more if asked to.
</details>
# 答案1
**得分**: 0
你无法通过这种方式访问映射中的结构体,因为可能没有结构体存在。在你的示例中,`VStructs["foo"]["bar"].Field1`会是什么呢?你无法知道它是否真的是映射中的空结构体,还是映射访问只是返回了该类型的空值。相反,你应该进行显式检查:
```go
v, ok := VStructs["01"]["Struct1"]
if !ok {
// 处理缺少结构体的情况。
}
// 检查类型断言也是一个好习惯。
s, ok := v.(V01.Struct1)
if !ok {
// 处理 v 不是 V01.Struct1 的情况。
}
f = s.Field1
英文:
You cannot access the struct inside a map this way because there may be no struct. In your example, what would VStructs["foo"]["bar"].Field1
be? You would have no way of knowing if it's really an empty struct in the map, or there is nothing and the map access just returned you an empty value for that type. What you should do instead is an explicit check:
v, ok := VStructs["01"]["Struct1"]
if !ok {
// Handle the case of the missing struct.
}
// It is also a good practice to check type assertions.
s, ok := v.(V01.Struct1)
if !ok {
// Handle the case when v is not V01.Struct1.
}
f = s.Field1
答案2
得分: 0
好的,以下是翻译好的内容:
好的,由于问题可能与使用golang的方式存在冲突。
我编写了一个包来满足我的需求:
根据选择的版本(在URL或标头中,任何你喜欢的方式),我们检索到一个结构体的映射。现在我们可以选择我们需要的实际结构体,设置一些值,最后将其发送回屏幕或发送到数据库(在我的情况下是gorp)。
该包可以在这里找到:https://github.com/donseba/contractor/
英文:
Okay, since the question was probably in conflict with the way golang is used to work.
I have written an package to serve my needs :
> Based on the selected version (In the URL or the Header, whatever you like) we retrieve an map of structs. We now can select the actual struct we need, set some values and finally send it back to the screen OR send it over to the DB (In my case gorp).
The Package can be found here https://github.com/donseba/contractor/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论