Re: [go-nuts] Map list of strings into memory

123 views
Skip to first unread message

Peter S

unread,
Nov 5, 2012, 10:43:54 PM11/5/12
to Lukáš Zapletal, golan...@googlegroups.com
If you don't insist on using Go and mmap to achieve your goal, you might want to try:

find / -print0 > the_file

It also has the advantage of working correctly when some file names contain newline characters.

Peter


On Tue, Nov 6, 2012 at 6:56 AM, Lukáš Zapletal <luk...@zapletalovi.com> wrote:
Hello,

I would like to map file created with

find / >the_file

(text file, each line one entry) into memory with golang and then iterate through it. For memory mapping there is

func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error)

which returns byte slice, now, to read a line I plan to reach byte by byte. Is there more effective approach?

Is there something if I would replace newlines in the file with zero bytes?

Thanks

--
 
 

Miguel Pignatelli

unread,
Nov 6, 2012, 5:03:39 AM11/6/12
to Lukáš Zapletal, golan...@googlegroups.com
On 05/11/12 21:56, Luk� Zapletal wrote:
> Hello,
>
> I would like to map file created with
>
> find / >the_file
>
> (text file, each line one entry) into memory with golang and then
> iterate through it. For memory mapping there is
>
> func Mmap(fd int, offset int64, length int, prot int, flags int) (data
> []byte, err error)
>
> which returns byte slice, now, to read a line I plan to reach byte by
> byte. Is there more effective approach?
>
> Is there something if I would replace newlines in the file with zero bytes?
>
> Thanks
>
> --
>

Why don't you use ioutil/ReadFile?
Or just open the file with and read line by line with bufio/ReadLine?

M;


>

Larry Clapp

unread,
Nov 6, 2012, 9:59:55 AM11/6/12
to golan...@googlegroups.com
Just to make sure: You know you could either just pipe find into your Go program:

find / | your_go_program

or open a pipe from find in your Go program (http://golang.org/pkg/os/#Pipe) directly, right?

No mmap required.

To answer your question, in the buffer package there are functions that allow you to read directly from the buffer using regular Read calls.

As for newlines -> zero bytes: find -print0. See also the find manpage for other printing options, e.g. -printf or -fprintf or -fprint0.

-- L

Larry Clapp

unread,
Nov 6, 2012, 10:19:23 AM11/6/12
to golan...@googlegroups.com
On Tuesday, November 6, 2012 9:59:55 AM UTC-5, Larry Clapp wrote:
To answer your question, in the buffer package there are functions that allow you to read directly from the buffer using regular Read calls.

I meant the bytes package.

-- L

Reply all
Reply to author
Forward
0 new messages