Type Assertion on File type

78 views
Skip to first unread message

André kouamé

unread,
May 7, 2020, 12:57:05 PM5/7/20
to golang-nuts
Hi,

I want to check, if the value return by my function has the type *os.File
This my code :
func createFile(filename string) (*os.File, error) {
f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
return f, err

}
//Test code
filename := "testfile"
f, _ := createFile(filename)
c := &f.(*os.File)
fmt.Println(c)
Error return :
invalid type assertion: f.(*os.File) (non-interface type *os.File on left)
Process exiting with code: 1

Andy Balholm

unread,
May 7, 2020, 1:37:27 PM5/7/20
to André kouamé, golang-nuts
The problem is that the function’s return type is already *os.File (rather than io.WriteCloser or some other interface), so the type assertion is pointless. 

Andy

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/e88406f6-7646-4cd9-9e0f-dfe6eae2581f%40googlegroups.com.

Marvin Renich

unread,
May 7, 2020, 1:46:08 PM5/7/20
to golang-nuts
* André kouamé <kouame...@gmail.com> [200507 12:57]:
The value being coerced must be an interface type. «f» above is of type
«*os.File», which is a concrete type. This playground link shows an
example of what I think you want:

https://play.golang.org/p/AILqjXUK0zx

Note that «rdr» is of type «io.Reader», which is an interface that
«*os.File» implements.

...Marvin

Amnon Baron Cohen

unread,
May 8, 2020, 2:23:02 AM5/8/20
to golang-nuts
The beautiful thing about Go is that it is statically typed,
so you don't need to check if your function returned an *os.File.
The compiler already did it  for you....

Brian Candler

unread,
May 8, 2020, 4:32:37 AM5/8/20
to golang-nuts
When you write:

f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)

the types of 'f' and 'err' are defined statically and magically as whatever the declared return types of os.OpenFile are.  It's like you wrote:

    var f type1
    var err type2
    f, err = os.OpenFile(...)

where type1 and type2 were replaced by looking at the function definition of os.OpenFile.

"return f, err" will then assign those values to the return types *your* function declared - which are *os.File and error respectively.  So if "f" or "err" are of the wrong type, you'll get a compile-time error, meaning you can't even build your program, let alone run it.  Example: https://play.golang.org/p/IOLMmMUGjCL

Therefore, there's no point checking the type of "f", since the compiler has already done this.

The interesting one is "err".  You are returning type "error", which is an interface type.  This means that "err" can have any concrete type which satisfies that interface, and it will work. Example: https://play.golang.org/p/Hdb9smP9hYh

Brian Candler

unread,
May 8, 2020, 4:40:56 AM5/8/20
to golang-nuts
Reply all
Reply to author
Forward
0 new messages