英文:
How to use interface methods in main file in go?
问题
我有一个在服务包中的接口
databaseService.go
package service
import "gitlab.com/xert/customerservice/internal/database/models"
type CustomerdetailsServiceInterface interface {
// Remove deletes a user by user name from database.
Add()
}
然后我有另一个服务文件customerDetailsService.go,在这里我正在实现接口方法
type CustomerdetailService struct {
}
func Add(ud *CustomerdetailService) {
fmt.Println("hello")
}
现在当我尝试在我的main.go中使用它时
import (
"gitlab.com/xert/customerservice/internal/database/service"
)
func main() {
service.CustomerdetailsServiceInterface.Add() // 这里报错,参数太少
}
如何像这样调用方法,实现其他文件中的方法?
英文:
I have an interface in the service package
databaseService.go
package service
import "gitlab.com/xert/customerservice/internal/database/models"
type CustomerdetailsServiceInterface interface {
// Remove deletes a user by user name from database.
Add()
}
Then I have another service file customerDetailsService.go,where i am implementing the interface methods
type CustomerdetailService struct {
}
func Add(ud *CustomerdetailService) {
fmt.Println("hello")
}
Now when I am trying to use it my main.go
import (
"gitlab.com/xert/customerservice/internal/database/service"
)
func main() {
service.CustomerdetailsServiceInterface.Add() // this is complaining too few arguments
}
How to call the method like this, implement methods in other file?
答案1
得分: 1
你一开始没有实现CustomerdetailsServiceInterface
。以下是你可以实现它的方法:
type CustomerdetailService struct {}
func (ud *CustomerdetailService) Add() {
fmt.Println("hello")
}
看一下下面的代码,希望能帮助你理解Go语言中的接口。
package main
import (
"fmt"
)
type CustomerdetailsServiceInterface interface {
Add()
}
type CustomerdetailService struct{}
func (ud *CustomerdetailService) Add() {
fmt.Println("hello")
}
type AnotherCustomerdetailService struct{}
func (ud *AnotherCustomerdetailService) Add() {
fmt.Println("Modified Addition")
}
func main() {
var x CustomerdetailsServiceInterface
x = &CustomerdetailService{}
x.Add()
x = &AnotherCustomerdetailService{}
x.Add()
}
你不能直接在接口上调用方法,但你需要一个具有实现该接口的类型的变量。在上面的例子中,CustomerdetailService
实现了CustomerdetailsServiceInterface
接口。
英文:
You have not implemented CustomerdetailsServiceInterface
in the first place. Following is the way you can implement it.
type CustomerdetailService struct {}
func (ud *CustomerdetailService) Add() {
fmt.Println("hello")
}
See the following code, hope it will help you understanding interface in go.
package main
import (
"fmt"
)
type CustomerdetailsServiceInterface interface {
Add()
}
type CustomerdetailService struct{}
func (ud *CustomerdetailService) Add() {
fmt.Println("hello")
}
type AnotherCustomerdetailService struct{}
func (ud *AnotherCustomerdetailService) Add() {
fmt.Println("Modified Addition")
}
func main() {
var x CustomerdetailsServiceInterface
x = &CustomerdetailService{}
x.Add()
x = &AnotherCustomerdetailService{}
x.Add()
}
You can not call a method directly on interface but you will need a variable with a type which implements the interface. In the above case it's CustomerdetailService
which implements CustomerdetailsServiceInterface
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论