Could not create temp file using ioutil - throw runtime error

217 views
Skip to first unread message

Jai Kumar

unread,
Sep 19, 2012, 6:07:23 PM9/19/12
to golan...@googlegroups.com
Why I am unable to create a temp file? Please see my code below.


or you can view code below.
 (both are same)

package main
import "fmt"
import "io/ioutil"

func main() {
tempFile, _ := ioutil.TempFile("/", "postcard_vid")
fmt.Println(tempFile.Name())
}


throws error:
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x8 pc=0x400c77]

goroutine 1 [running]:
main.main()
	/tmpfs/gosandbox-0656bfdd_990cb295_da1c8f79_6e849913_24d9a636/prog.go:8 +0x77

Dave Cheney

unread,
Sep 19, 2012, 6:11:19 PM9/19/12
to Jai Kumar, golan...@googlegroups.com
You are discarding the error value returned form ioutil.TempFile. If
there is an error (and there is good reason to believe there is) then
you cannot make any assumptions about the value of tempFile.

Cheers

Dave
> --
>
>

Dan Kortschak

unread,
Sep 19, 2012, 6:12:50 PM9/19/12
to Jai Kumar, golan...@googlegroups.com
Have a look at the error.

Jai Kumar

unread,
Sep 19, 2012, 6:15:34 PM9/19/12
to golan...@googlegroups.com, Jai Kumar
Thanks. Got it. I checked error and it was permission issue. Thank you. I fixed it now. You guys are awesome :)

Richard Norton

unread,
Sep 19, 2012, 6:15:13 PM9/19/12
to Dan Kortschak, Jai Kumar, golan...@googlegroups.com
Right.

You don't have permission to write to the root dir (that's what my system reported at any rate).

Try using "" for the dir string instead of "/"; that should use the system default dir for temp files.

Richard
> --
>
>

Johann Höchtl

unread,
Sep 20, 2012, 3:16:15 PM9/20/12
to golan...@googlegroups.com, Jai Kumar


Am Donnerstag, 20. September 2012 00:15:34 UTC+2 schrieb Jai Kumar:
Thanks. Got it. I checked error and it was permission issue. Thank you. I fixed it now. You guys are awesome :)
 
Thanks god it was a permission issue, otherwise you would be executing  as root which is generally a bad idea. 

Reply all
Reply to author
Forward
0 new messages