Re: [go-nuts] How Does This Work?

2022-03-06 Thread Rob Pike
Be careful. You are correct but newcomers to Go often get confused here. Printf unpacks the interface it is passed to find the concrete element inside. Doing what reflect requires you to do to discover the type of an interface is unrelated to the problem at hand. Watch: % cat x.go package main

Re: [go-nuts] How Does This Work?

2022-03-06 Thread Martin Schnabel
one important detail i have not found in the other answers is that stat is declared to return a fs.FileInfo, but printf takes an empty interface argument and therefor unwraps the interface value. so if you want to print the actual variable type of interface variables you can pass a pointer (same i

Re: [go-nuts] How Does This Work?

2022-03-06 Thread Kurtis Rader
On Sun, Mar 6, 2022 at 12:33 PM jlfo...@berkeley.edu wrote: > Thanks for the quick reply. Let's say I wanted to pass "fileinfo" from my > program to another function. What would I say in function's argument list > for fileinfo's type? From your reply I'm guessing I would give "fileinfo > *io/fs.f

[go-nuts] Re: How Does This Work?

2022-03-06 Thread Brian Candler
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 o

Re: [go-nuts] How Does This Work?

2022-03-06 Thread jlfo...@berkeley.edu
Thanks for the quick reply. Let's say I wanted to pass "fileinfo" from my program to another function. What would I say in function's argument list for fileinfo's type? From your reply I'm guessing I would give "fileinfo *io/fs.fileinfo" but I'm not sure. Jon On Sunday, March 6, 2022 at 12:2

Re: [go-nuts] How Does This Work?

2022-03-06 Thread Kurtis Rader
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 prohib

[go-nuts] How Does This Work?

2022-03-06 Thread jlfo...@berkeley.edu
(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