It's because os.FileInfo is an interface, not a concrete type. Specifically, it's an alias for io/fs.FileInfo. The concrete type returned by os.Stat() is os.fileStat; a struct that implements the os.FileInfo interface. While you can't refer to a private type like os.fileStat there is nothing prohibiting the os package from returning a private type that satisfies an interface. See, for example, https://github.com/golang/go/blob/45f45444b307cea7c8330b100b30382e642e010f/src/os/stat_unix.go#L15-L26
On Sun, Mar 6, 2022 at 11:47 AM jlfo...@berkeley.edu <jlforr...@berkeley.edu> wrote: > (go version go1.17.7 linux/amd64) > > Consider the following trivial program: > ------ > package main > > import ( > "fmt" > "os" > ) > > func main() { > file := "." > fileinfo, _ := os.Stat(file) > fmt.Printf("type of fileinfo = %T\n", fileinfo) > } > ------ > > This runs and produces the output > > type of fileinfo = *os.fileStat > > Fine, but notice that "fileStat" isn't capitalized. This means this symbol > isn't > exported outside the "os" package. Yet, somehow the "fileinfo" variable is > assigned > this type. > > Indeed, if I try to explicitly use the "os.fileStat" type in the program, > the program fails to compile, e.g. > > ------------ > package main > > import ( > "fmt" > "os" > ) > > func main() { > var fileinfo *os.fileStat > > file := "." > fileinfo, _ = os.Stat(file) > fmt.Printf("type of fileinfo = %T\n", fileinfo) > } > ----- > results in > > ./x3.go:9:16: cannot refer to unexported name os.fileStat > ./x3.go:12:14: cannot assign fs.FileInfo to fileinfo (type *os.fileStat) > in multiple assignment: need type assertion > ./x3.go:12:14: cannot use fs.FileInfo value as type *os.fileStat in > assignment: need type assertion > > Notice the first error message. > > I also don't understand why the other two error message are produced when > all I did was to explicitly declare a variable that was previously assigned > a value in a short declaration. > > What am I missing? > > Cordially, > Jon Forrest > > > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/aef48e1e-e0c7-44e4-b62b-2fd1f6e5157bn%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/aef48e1e-e0c7-44e4-b62b-2fd1f6e5157bn%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- Kurtis Rader Caretaker of the exceptional canines Junior and Hank -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD-A9xsQ3eXKrD%2B7xDuYX5Ep_xW%2Bfrxirgj%2BqtypkxWJ_g%40mail.gmail.com.