https://go.dev/play/p/pz02pSaVCyl
A function can return a value of a private type (or a pointer to such a type, as you found). It's a way to return an opaque value that implements an interface. The receiver in a different package can assign that value, but can't declare additional variables of that type. They can of course call public methods on values of that type - or assign to a variable of a compatible interface type. On Sunday, 6 March 2022 at 19:46:18 UTC jlfo...@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/e07af59c-7205-4732-83d2-ef352b91da0bn%40googlegroups.com.