Golang接口和接收器-需要建议
I'm trying to convert my config loader class in Golang from a specific config file structure to a more general one. Originally, I defined a struct with a set of program-specific variables, for example:
type WatcherConfig struct {
FileType string
Flag bool
OtherType string
ConfigPath string
}
I then defined two methods with pointer receivers:
func (config *WatcherConfig) LoadConfig(path string) error {}
and
func (config *WatcherConfig) Reload() error {}
I'm now attempting to make this more general, and the plan was to define an interface Config
and define the LoadConfig
and Reload
methods on this. I could then create a struct
with the config layout for each module that needed it, and save myself repeating a method that basically opens a file, reads JSON, and dumps it into a struct.
I've tried creating an interface and defining a method like this:
type Config interface {
LoadConfig(string) error
}
func (config *Config) LoadConfig(path string) error {}
But that is obviously throwing errors as Config
is not a type, it's an interface. Do I need to add a more abstract struct
to my class? It may be useful to know that all configuration structs will have the ConfigPath
field, as I use this to Reload()
the config.
I'm fairly sure I'm going about this the wrong way, or what I'm trying to do isn't a pattern that works nicely in Go. I'd really appreciate some advice!
- Is what I'm trying to do possible in Go?
- Is it a good idea in Go?
- What would be the alternative Go-ism?
我正在尝试将Golang中的配置加载程序类从特定的配置文件结构转换为更通用的结构。 最初,我定义了一个具有一组程序特定变量的结构,例如: p>
type WatcherConfig结构{
FileType字符串
标志bool
OtherType字符串
ConfigPath字符串
}
code> pre>
然后我定义了两个带有指针接收器的方法: p>
func(config * WatcherConfig )LoadConfig(路径字符串)错误{}
code> pre>
和 p>
func(config * WatcherConfig)Reload() 错误{}
code> pre>
我现在正在尝试使其更加通用,并且计划是定义接口 Config code>并定义 LoadConfig code>和 Reload code>方法。 然后,我可以为每个需要它的模块使用config布局创建一个 struct code>,并避免重复一次基本上打开文件,读取JSON并将其转储到struct中的方法。 p>
我尝试创建一个接口并定义如下方法: p>
type Config interface {
LoadConfig(string)error
} \ nfunc(config * Config)LoadConfig(路径字符串)错误{}
code> pre>
但这显然会引发错误,因为 Config code>不是一种类型 ,这是一个界面。 我需要在课堂上添加一个更抽象的 struct code>吗? 了解 strong>可能是有用的 strong>,因为我将其用于 Reload() code>配置,所以所有配置结构都将具有 ConfigPath code>字段。 p>
我很确定我会以错误的方式进行操作,或者我要尝试的不是在Go中运行良好的模式。 我真的很感谢一些建议! p>
- 我想在Go中做些什么吗? li>
- 在Go中这是个好主意吗? li> \ n
- 什么是替代的Go-ism? li>
ul>
div>
Even if you'd use embedding both the interfaces and the implementations, the implementation of Config.LoadConfig()
cannot know about the type that embeds it (e.g. WatcherConfig
).
Best would be not to implement this as methods but as simple helper or factory functions.
You could do it like this:
func LoadConfig(path string, config interface{}) error {
// Load implementation
// For example you can unmarshal file content into the config variable (if pointer)
}
func ReloadConfig(config Config) error {
// Reload implementation
path := config.Path() // Config interface may have a Path() method
// for example you can unmarshal file content into the config variable (if pointer)
}