FAQ
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

--

Search Discussions

  • Peter S at Nov 6, 2012 at 3:43 am
    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 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 at Nov 6, 2012 at 10:03 am

    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 at Nov 6, 2012 at 3:00 pm
    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

    On Monday, November 5, 2012 4:56:58 PM UTC-5, 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
    --
  • Larry Clapp at Nov 6, 2012 at 3:19 pm

    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

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 5, '12 at 11:17p
activeNov 6, '12 at 3:19p
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase