In the below code the first printAll
has a compile error ./main.go:10:7: cannot use info (type []fs.FileInfo) as type fileInfoList in argument to print
. How come this is the case? Shouldn't the fileInfo
interface be met since each fs.FileInfo
type has a Name
method?
package main
import (
"fmt"
"io/ioutil"
)
func main() {
info, _ := ioutil.ReadDir("./")
// info is of type []fs.FileInfo
/*
type FileInfo interface {
Name() string
// ...
}
*/
printAll(info) // DOESN'T WORK
var list []fileInfo
for _, f := range info {
list = append(list, f)
}
printAll(list) // works
d1 := defaultFileInfo{}
d2 := defaultFileInfo{}
dList := []fileInfo{&d1, &d2}
printAll(dList) // works
}
type fileInfo interface {
Name() string
}
func printAll(fileInfoList []fileInfo) {
for _, f := range fileInfoList {
fmt.Println(f.Name())
}
}
type defaultFileInfo struct{}
func (d *defaultFileInfo) Name() string {
return "..."
}