(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