Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning File Pointer in Golang

Tags:

pointers

go

I'm still struggling with the basics of Golang.

Consider the following sample code:

func OpenOutputFile(name string) (fp *os.File) {
  fp, err := os.Create(name)
  if err != nil {
      panic(err)
  }

  defer func() {
      if err := fp.Close(); err != nil {
          panic(err)
      }
  }()

  return fp
}

I would assume that calling:

fp := OpenOutputFile("output.txt")

would now make fp a file pointer (*os.File), so that I could call a statement like:

io.WriteString(fp, "Hello World")

In another function. But when calling this method, the error is generated:

0 write output.txt: bad file descriptor

So it appears that the pointer returned is not valid. How can I return a properly formed pointer to use with io.WriteString?

I appreciate the help!

Of note: Everything executes as intended when the creation of the file pointer and the writing to the file pointer exists in the same method. Breaking the logic into a function causes it to not behave as intended.

like image 769
James Taylor Avatar asked Mar 20 '16 03:03

James Taylor


1 Answers

The Go Programming Language Specification

Defer statements

A "defer" statement invokes a function whose execution is deferred to the moment the surrounding function returns, either because the surrounding function executed a return statement, reached the end of its function body, or because the corresponding goroutine is panicking.

Each time a "defer" statement executes, the function value and parameters to the call are evaluated as usual and saved anew but the actual function is not invoked. Instead, deferred functions are invoked immediately before the surrounding function returns, in the reverse order they were deferred. If a deferred function value evaluates to nil, execution panics when the function is invoked, not when the "defer" statement is executed.

For instance, if the deferred function is a function literal and the surrounding function has named result parameters that are in scope within the literal, the deferred function may access and modify the result parameters before they are returned. If the deferred function has any return values, they are discarded when the function completes.

func OpenOutputFile(name string) (fp *os.File) {
    fp, err := os.Create(name)
    if err != nil {
        panic(err)
    }

    defer func() {
        if err := fp.Close(); err != nil {
            panic(err)
        }
    }()

    return fp
}

You open the file

fp, err := os.Create(name)

You close the file

err := fp.Close()

After the Close, fp no longer points to a valid file descriptor.

like image 98
peterSO Avatar answered Nov 05 '22 11:11

peterSO