英文:
Compilation error: method call through an interface
问题
我正在学习Go语言的接口,并尝试实现以下代码,但是在VSCode中显示编译错误,请求帮助。我漏掉了什么?谢谢。
package dataaccess
import (
"../domain"
)
type IProductDataAccess interface {
GetProducts() ([]*domain.Product, error)
GetProduct(string) (*domain.Product, error)
}
var (
ProductDataAccess IProductDataAccess
)
type productDAO struct{}
func init() {
ProductDataAccess = new(productDAO)
}
func (p *productDAO) GetProduct(productID string) (*domain.Product, error) {
return nil, nil
}
func (p *productDAO) GetProducts() ([]*domain.Product, error) {
return nil, nil
}
在另一个包中的组件中,当对IProductDataAccess进行方法调用时,编辑器中出现以下编译错误:
调用dataaccess.IProductDataAccess.GetProduct时参数不足
已有(string)
期望(dataaccess.IProductDataAccess, string) (WrongArgCount)
调用dataaccess.IProductDataAccess.GetProducts时参数不足
已有()
期望(dataaccess.IProductDataAccess) (WrongArgCount)
package service
import (
"../dataaccess"
"../domain"
)
type IProductService interface {
GetProducts() ([]*domain.Product, error)
GetProduct(string) (*domain.Product, error)
}
var (
ProductService IProductService
)
type productService struct{}
func init() {
ProductService = new(productService)
}
func (p *productService) GetProduct(productID string) (*domain.Product, error) {
product, err := dataaccess.IProductDataAccess.GetProduct(productID)
if err != nil {
return nil, err
}
return product, nil
}
func (p *productService) GetProducts() ([]*domain.Product, error) {
products, err := dataaccess.IProductDataAccess.GetProducts()
if err != nil {
return nil, err
}
return products, nil
}
英文:
I am learning go interfaces and trying to implement the below but vscode is showing me a compilation error, requesting help. What am I missing. Thank you.
package dataaccess
import (
"../domain"
)
type IProductDataAccess interface {
GetProducts() ([]*domain.Product, error)
GetProduct(string) (*domain.Product, error)
}
var (
ProductDataAccess IProductDataAccess
)
type productDAO struct{}
func init() {
ProductDataAccess = new(productDAO)
}
func (p *productDAO) GetProduct(productID string) (*domain.Product, error) {
return nil, nil
}
func (p *productDAO) GetProducts() ([]*domain.Product, error) {
return nil, nil
}
Getting the following compilation errors in the editor when making the method calls on the IProductDataAccess from a component as shown below in a different package
> not enough arguments in call to dataaccess.IProductDataAccess.GetProduct
have (string)
want (dataaccess.IProductDataAccess, string) (WrongArgCount)
> not enough arguments in call to dataaccess.IProductDataAccess.GetProducts
have ()
want (dataaccess.IProductDataAccess)compiler (WrongArgCount)
package service
import (
"../dataaccess"
"../domain"
)
type IProductService interface {
GetProducts() ([]*domain.Product, error)
GetProduct(string) (*domain.Product, error)
}
var (
ProductService IProductService
)
type productService struct{}
func init() {
ProductService = new(productService)
}
func (p *productService) GetProduct(productID string) (*domain.Product, error) {
product, err := dataaccess.IProductDataAccess.GetProduct(productID)
if err != nil {
return nil, err
}
return product, nil
}
func (p *productService) GetProducts() ([]*domain.Product, error) {
products, err := dataaccess.IProductDataAccess.GetProducts()
if err != nil {
return nil, err
}
return products, nil
}
答案1
得分: 2
这一行看起来需要调整:
product, err := dataaccess.IProductDataAccess.GetProduct(productID)
dataaccess.IProductDataAccess
是一个接口,不是一个可以调用的实际对象。dataaccess.ProductDataAccess
是页面上你想要调用的单例实例。
我认为你真正想要的是:
product, err := dataaccess.ProductDataAccess.GetProduct(productID)
对于 GetProducts 调用也是类似的:
products, err := dataaccess.ProductDataAccess.GetProducts()
英文:
This line looks like it needs adjustment:
product, err := dataaccess.IProductDataAccess.GetProduct(productID)
dataaccess.IProductDataAccess
is an interface, not an actual object to invoke a call on. dataaccess.ProductDataAccess
is the singleton instance on that page that what you want to invoke.
I think you really want:
product, err := dataaccess.ProductDataAccess.GetProduct(productID)
Similarly for the GetProducts call:
products, err := dataaccess.ProductDataAccess.GetProducts()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论