Compilation error: method call through an interface
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 进行方法调用时,在编辑器中出现以下编译错误
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
}
这条线看起来需要调整:
product, err := dataaccess.IProductDataAccess.GetProduct(productID)
dataaccess.IProductDataAccess
是一个接口,而不是调用调用的实际对象。 dataaccess.ProductDataAccess
是您要调用的页面上的单例实例。
我想你真的想要:
product, err := dataaccess.ProductDataAccess.GetProduct(productID)
与 GetProducts 调用类似:
products, err := dataaccess.ProductDataAccess.GetProducts()
我正在学习 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 进行方法调用时,在编辑器中出现以下编译错误
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
}
这条线看起来需要调整:
product, err := dataaccess.IProductDataAccess.GetProduct(productID)
dataaccess.IProductDataAccess
是一个接口,而不是调用调用的实际对象。 dataaccess.ProductDataAccess
是您要调用的页面上的单例实例。
我想你真的想要:
product, err := dataaccess.ProductDataAccess.GetProduct(productID)
与 GetProducts 调用类似:
products, err := dataaccess.ProductDataAccess.GetProducts()