Example code:
type IClient interface {
UploadFile(sourcePath, host string) error
CopyFile(sourcePath, destPath string) error
DeleteFile(sourcePath, option string) error
GetChecksum(sourcePath string) (*string, error)
GetSize(sourcePath string) (*float32, error)
}
type FileWorker struct {
Extension string
Host string
}
func NewFileWorker(fileExtension, host string) IClient {
var fileWorker = &FileWorker {
Extension: extension,
Host: host,
}
return fileWorker
}
I have seen other "New" (or "Get") function returning interface like above. But it does not actually return the interface, but instead a struct that implements said interface. In turn, that struct type is the receiver of various methods implementing said interface.
Question: When and why do we return interfaces? Currently I see this is a roundabout way of doing things, resulting in one having to trace back through multiple files while trying to understand some codes.
Aucun commentaire:
Enregistrer un commentaire