Which method is better for reader file (txt, xml)

3,205 views
Skip to first unread message

Amitabh Arya

unread,
Mar 20, 2012, 2:51:28 PM3/20/12
to golan...@googlegroups.com
What is the difference between 
ioutil.ReadFile()

and 

txtFile, err := os.OpenFile(path, os.O_RDONLY, 0666)
fLen, err2 := txtFile.Stat()
data := make([]byte, fLen.Size())
txtFile.Read(data)

And which one faster and more reliable.

minux

unread,
Mar 20, 2012, 2:56:39 PM3/20/12
to Amitabh Arya, golan...@googlegroups.com
You can see for yourself. http://weekly.golang.org/src/pkg/io/ioutil/ioutil.go
ioutil.ReadFile() is almost the same with your second approach.

Brad Fitzpatrick

unread,
Mar 20, 2012, 2:57:19 PM3/20/12
to Amitabh Arya, golan...@googlegroups.com
Between those two options? Use ioutil.ReadFile.

Also, why would you use os.OpenFile(path, os.O_RDONLY, 0666) instead of os.Open(path)?  Your 0666 is ignored anyway.  Also, you ignore both your errors.  Also, you trust the return value of Read.  Unlike Write, Read only has to return something. It doesn't have to fill the buffer.  See the docs on io.Reader, and see io.ReadAll.

In short, your version has many bugs.  ioutil.ReadFile is solid.

Matt Kane's Brain

unread,
Mar 20, 2012, 3:01:23 PM3/20/12
to Amitabh Arya, golan...@googlegroups.com
The latter is basically what ioutil.ReadFile() does, but won't
pre-allocate a buffer bigger than 2 GB. (which is still impossible
while int is still 32 bits wide and slices and arrays use int as the
index type)

ioutil.ReadFile() also defers a call to Close() to clean up the
filehandle. Looks like it might panic if there's a read error aside
from the file being too enormous?

--
matt kane's brain
http://hydrogenproject.com

Amitabh Arya

unread,
Mar 20, 2012, 3:13:34 PM3/20/12
to Brad Fitzpatrick, golan...@googlegroups.com
if   os.Open(path) and ioutil.ReadFile(path) can do same things as os.OpenFile(path, os.O_RDONLY, 0666)  does, then Why it is there. Like three methods for the same functionality. WHY? 

There should be some positive or negative each one should have?

Thanks,
Amitabh Arya

DisposaBoy

unread,
Mar 20, 2012, 3:25:22 PM3/20/12
to golan...@googlegroups.com, Brad Fitzpatrick
They don't provide the same functionality?
The existense of Open and ReadFile is a good thing(convenience) - as demonstrated by your code. I don't think you were questioning the existense of OpenFile as that'd make no sense.

Amitabh Arya

unread,
Mar 20, 2012, 3:34:54 PM3/20/12
to golan...@googlegroups.com, Brad Fitzpatrick
Yeah I got the Answer :) thanks buddies.  

ioutil.Readfile has already  coded what I wrote. So just call ioutil.ReadFile and save your 4-5 lines of code. So surely it's a utility function.

chris dollin

unread,
Mar 20, 2012, 4:14:52 PM3/20/12
to Amitabh Arya, Brad Fitzpatrick, golan...@googlegroups.com
On 20 March 2012 19:13, Amitabh Arya <amitab...@gmail.com> wrote:
> if   os.Open(path) and ioutil.ReadFile(path) can do same things
> as os.OpenFile(path, os.O_RDONLY, 0666)  does, then Why it is there. Like
> three methods for the same functionality. WHY?

os.Open(path) avoids the clutter of the `os.O_RDONLY, 0666` arguments
and reduces opportunities to make a mistake in a common case.

ioutil.ReadFile likewise: it's an operation that wants to be done sufficiently
often, and right, that it's worth having it in the library.

[It also reduces question-load, which is not an insignificant benefit.]

Chris

--
Chris "allusive" Dollin

Hotei

unread,
Mar 20, 2012, 4:50:26 PM3/20/12
to golan...@googlegroups.com, Amitabh Arya
mkb,
ReadFile doesn't panic when I feed it a 5 GB file. Came back with an error of course but it didn't give up. That's good.

Matt Kane's Brain

unread,
Mar 20, 2012, 4:56:49 PM3/20/12
to Hotei, golan...@googlegroups.com, Amitabh Arya
Yes, but that's not what I said. But what I said is wrong too. It only
panics if bytes.ReadFrom panics
Reply all
Reply to author
Forward
0 new messages