How to use interface methods in main file in go?

huangapple go评论67阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2021年8月7日 14:47:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/68690026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定