我正在學(xué)習(xí) go 接口并嘗試實(shí)現(xiàn)以下內(nèi)容,但 vscode 向我顯示編譯錯(cuò)誤,請(qǐng)求幫助。我錯(cuò)過(guò)了什么。謝謝你。package dataaccessimport ( "../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}從如下所示的不同包中的組件對(duì) IProductDataAccess 進(jìn)行方法調(diào)用時(shí),在編輯器中出現(xiàn)以下編譯錯(cuò)誤調(diào)用 dataaccess.IProductDataAccess.GetProduct 時(shí)沒(méi)有足夠的參數(shù)有(字符串)想要(dataaccess.IProductDataAccess,字符串)(WrongArgCount)調(diào)用 dataaccess.IProductDataAccess.GetProducts 時(shí)沒(méi)有足夠的參數(shù)有 () want (dataaccess.IProductDataAccess)compiler (WrongArgCount)package serviceimport ( "../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}
編譯錯(cuò)誤:通過(guò)接口調(diào)用方法
寶慕林4294392
2022-12-13 10:28:47