英文:
Golang implementing database funcs using interfaces
问题
抱歉,我只能回答中文问题。以下是翻译好的内容:
对于一些愚蠢的问题,我感到抱歉,但我有些困惑。
所以,我正在为我的应用程序实现一个数据库驱动的包装器,并且我需要尽可能地保持它的可移植性。
我决定使用接口来完成这个任务。所以,我有一个包含一些变量和特定于应用程序的方法的数据库结构,以及两个接口函数:
query(request string) error
flush() int? string?? struct?? slice????, error
现在你可能有一个主要的问题。如何返回类型为"flush()"不知道的数据?我可以通过接口返回它吗?如果可以,如何处理它?
第二个问题相当基础,但对我来说仍然不清楚。
所以,我有这个数据库结构,其中有两个方法,设计成由包用户来实现,以使用他想要的数据库驱动。
我应该如何编写它,未来的实现会是什么样子(在Go之旅中有一个例子,但它是关于具有相似方法的不同结构的接口)。
希望你能帮助我理解
英文:
Sorry for a bit dummy questions, but I'm kinda stuck.
So, I'm implementing wrapper above database driver for my application, and I need to keep it as portable as possible.
I came to decision that interfaces are perfect match for this task. So, I have my database struct with some variables and app-specific methods, and two interface functions:
query(request string) error
flush() int? string?? struct?? slice????, error
Now you probably got the main question. How do I do return data that type "flush()" doesn't know? Can I return it by interface, and if I can, how to work with that?
Second question is pretty basic, but still it isn't clear to me.
So, I have this database struct with two methods designed to be implemented by package user to use db driver he want. <br>
Ho do I write it and how future implementation will look (there is an example on tour of go, but it's about interface for different structs with similar methods)<br>
Hope you'll help me find an understanding
答案1
得分: 0
是的,flush只需要具有以下签名:
flush() (interface{}, error)
你可以像这样实现它,合理的方法体应该适用于你:
type MyDbDriver struct {
// 字段
}
func (d *MyDbDriver) query(request string) error {
return nil
}
func (d *MyDbDriver) flush() (interface{}, error) {
return nil, nil
}
在Go中,所有接口实现都是隐式的,也就是说,如果你的类型具有与接口签名匹配的方法,那么你已经实现了该接口。不需要像 public class MyType: IMyInterface, IThisIsntCSharp
这样的写法。请注意,在上面的示例中,*MyDbDriver
实现了你的接口,但 MyDbDriver
没有实现。
以下是一些伪代码示例:
e := driver.query("select * from thatTable where ThatProperty = 'ThatValue'")
if e != nil {
return nil, e
}
i, err := driver.flush()
if err != nil {
return nil, err
}
MyConcreteInstance := i.(MyConcreteType)
// 注意,如果 i 的类型不是 MyConcreteType,这将引发 panic
// 可以使用熟悉的对象、err 调用语法来避免这种情况
MyConcreteIntance, ok := i.(MyConcreteType)
if !ok {
// i 的类型不是 MyConcreteType :\
}
希望对你有所帮助!
英文:
Yes, flush can just have the signiture;
flush() interface{}, error
How do you implement? Something like this with reasonable method bodies should do it for you;
type MyDbDriver struct {
//fields
}
func (d *MyDbDriver) query(request string) error {
return nil
}
func (d *MyDbDriver) flush() interface{}, error {
return nil, nil
}
In Go all interface implementations are implicit, meaning, if your type has methods that match the interfaces signature, then you've implemented it. No need for something like public class MyType: IMyInterface, IThisIsntCSharp
. Note that in the example above *MyDbDriver
has implemented your interface but MyDbDriver
has not.
Edit: below a some pseudo calling code;
e := driver.query("select * from thatTable where ThatProperty = 'ThatValue'")
if e != nil {
return nil, e
}
i, err := driver.flush()
if err != nil {
return nil, err
}
MyConcreteInstance := i.(MyConcreteType)
// note that this will panic if the type of i is not MyConcreteType
// that can be avoided with the familiar object, err calling syntax
MyConcreteIntance, ok := i.(MyConcreteType)
if !ok {
// the type of i was not MyConcreteType :\
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论