英文:
Golang return map[string]interface{} returning variable struct
问题
我需要一个大的结构体表,并且我需要在返回的结构体上进行操作。
package main
import (
"fmt"
)
var factory map[string]interface{} = map[string]interface{}{
"Date": Date{},
"DateTime": DateTime{},
}
type Date struct {
year int //xsd:int Year (e.g., 2009)
month int //xsd:int Month (1..12)
day int //xsd:int Day number
}
func (d *Date) Init() {
d.year = 2009
d.month = 1
d.day = 1
}
type DateTime struct {
date Date //Date
hour int //xsd:int
minute int //xsd:int
second int //xsd:int
timeZoneID string //xsd:string
}
func (d *DateTime) Init() {
d.hour = 0
d.minute = 0
d.second = 0
}
func main() {
obj := factory["Date"]
obj.Init()
fmt.Println(obj)
}
我得到了错误信息 "obj.Init undefined (type interface {} is interface with no methods)"。有没有办法解决这个问题?
英文:
I need a big table of structs and I need to work off the struct returned.
package main
import (
"fmt"
)
var factory map[string]interface{} = map[string]interface{}{
"Date": Date{},
"DateTime": DateTime{},
}
type Date struct {
year int //xsd:int Year (e.g., 2009)
month int //xsd:int Month (1..12)
day int //xsd:int Day number
}
func( d *Date ) Init(){
d.year = 2009
d.month = 1
d.day = 1
}
type DateTime struct {
date Date //Date
hour int //xsd:int
minute int //xsd:int
second int //xsd:int
timeZoneID string //xsd:string
}
func( d *DateTime ) Init(){
d.hour = 0
d.minute = 0
d.second = 0
}
func main() {
obj := factory["Date"]
obj.Init()
fmt.Println( obj )
}
Go Playground
but I get the error obj.Init undefined (type interface {} is interface with no methods) Is there a way to do this?
答案1
得分: 2
基本上,你需要告诉编译器你的所有类型(映射中的实例)都将始终具有一个Init方法。为此,你声明一个带有Init方法的接口,并构建该接口的映射。
由于你的接收器在指针*xxx上工作,所以你需要通过在它们前面添加&来将对象的指针添加到映射中(而不是对象本身)。
package main
import (
"fmt"
)
type initializer interface {
Init()
}
var factory map[string]initializer = map[string]initializer{
"Date": &Date{},
"DateTime": &DateTime{},
}
type Date struct {
year int //xsd:int Year (e.g., 2009)
month int //xsd:int Month (1..12)
day int //xsd:int Day number
}
func (d *Date) Init() {
d.year = 2009
d.month = 1
d.day = 1
}
type DateTime struct {
date Date //Date
hour int //xsd:int
minute int //xsd:int
second int //xsd:int
timeZoneID string //xsd:string
}
func (d *DateTime) Init() {
d.hour = 0
d.minute = 0
d.second = 0
}
func main() {
obj := factory["Date"]
obj.Init()
fmt.Println(obj)
}
英文:
Basically, you need to tell the compiler that all your types (instances in the map) will always have an Init method. For that you declare an interface with the Init method and build a map of that interface.
Since your receivers work on a pointer *xxx, you need to add the pointers of the objects to the map (not the objects themselves) by adding & in front of them.
package main
import (
"fmt"
)
type initializer interface {
Init()
}
var factory map[string]initializer = map[string]initializer{
"Date": &Date{},
"DateTime": &DateTime{},
}
type Date struct {
year int //xsd:int Year (e.g., 2009)
month int //xsd:int Month (1..12)
day int //xsd:int Day number
}
func (d *Date) Init() {
d.year = 2009
d.month = 1
d.day = 1
}
type DateTime struct {
date Date //Date
hour int //xsd:int
minute int //xsd:int
second int //xsd:int
timeZoneID string //xsd:string
}
func (d *DateTime) Init() {
d.hour = 0
d.minute = 0
d.second = 0
}
func main() {
obj := factory["Date"]
obj.Init()
fmt.Println(obj)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论