英文:
Golang import based on config variable
问题
我目前正在学习Go语言,最近在我的一个测试中遇到了一个问题:我想创建一个快速备份应用程序,该应用程序将引用一个配置文件,并根据配置文件中的"plugin"来切换备份方式。目前我创建了以下代码(仅为示例,可能存在语法错误):
type BackupStorage interface {
Put(d []byte) (n int, err Error)
Get(l []byte) (d []byte, err Error)
}
在这一点上,我认为我应该使用反射来根据类型切换并返回特定的备份函数,尽管这似乎不太正确。
我想到的第二种方法是仍然使用BackupStorage接口,并创建"plugin"包来指定使用哪个导入包,但是如何根据配置文件变量进行切换呢?我希望能够根据配置文件选择不同的存储后端,因为某些机器上的一些内容可能只备份到S3,而其他机器可能同时备份到S3和Google Drive等。
根据上面的基本示例,我想实现以下功能:实现两个BackupStorage "plugins"(Google Drive / S3),并具有随时添加更多插件的灵活性,但同时使我的代码足够通用,可以在配置文件中选择任何存储后端来执行。
正如我上面提到的,我还在学习中,希望能够得到解释或者示例来完成这个任务。如果我做错了,我不介意得到"你做错了"的回答,只要能够给出正确的解释和正确的做法即可。
英文:
I'm currently in the process of still learning go but I recently got to a point where in one of my tests I wanted to create a quick backup application that will reference a config file and switch what "plugin" to use for the backup. So what I got to at this point is to create (as example, written from my head and syntax may be incorrect):
type BackupStorage interface{
Put (d []byte) (n int, err Error)
Get (l []byte) (d []byte, err Error)
}
At this point I would assume I should use reflection to switch on the type and return the specific backup function, although that does not seem right.
The second alternative I came to was to still use my BackupStorage interface and create "plugin" packages to dictate which import will be used, but how do I switch that then based on a config file variable. And I'd like to do this because some stuff on one machine may backup only to s3 where as others may backup to both s3 and google drive etc.
With the basic example above what I have in mind is this:
Implement 2 BackupStorage "plugins" (Google Drive / S3) with the flexibility to add more at any time, but have my code be generic enough to execute on whatever storage backend is selected in config files.
As I mentioned above I'm still learning and any explanations would be appreciated or examples on how to accomplish this. I don't mind the "your doing it wrong" as long as there is a proper explanation on why it's wrong and how to do it right.
答案1
得分: 4
你有一个正确的想法,首先通过接口实现所需的一切,然后可以插入任何实现该接口的具体备份“插件”。
一旦你可以通过接口运行备份,你可以根据你设置的条件简单地分配所需的后端实例。
var storage Backupper
type Backupper interface {
Backup()
}
type GDrive struct {
config string
}
func (g *GDrive) Backup() {
fmt.Println("Doing backup to Google Drive")
}
func main() {
storage = &GDrive{}
storage.Backup()
}
或者使用多个选项:http://play.golang.org/p/RlmXjf55Yh
英文:
You have the right idea to start, implement everything you need via an interface, and then you can plug in any concrete backup "plugin" that implements that interface.
Once you can run your backup via an interface, you can simply assign an instance of the backend you want based on whatever conditions you set.
var storage Backupper
type Backupper interface {
Backup()
}
type GDrive struct {
config string
}
func (g *GDrive) Backup() {
fmt.Println("Doing backup to Google Drive")
}
func main() {
storage = &GDrive{}
storage.Backup()
}
Or with multiple options: http://play.golang.org/p/RlmXjf55Yh
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论