-2

Similar to this question, what is the idiomatic way to process a file one byte at a time in go?

Put another way, is there a better way to write the following?

file, err := ioutil.ReadFile(filename)
file_string = string(file)
for i, c := range file_string {
    // -snip-
}
2
  • 5
    This isn't reading the file one byte at a time. This is reading the whole file, converting it to a string, and iterating through it rune by rune (not byte by byte). Do you want to read the file one byte at a time, or process it one byte at a time? Commented Apr 30, 2020 at 16:23
  • 3
    Reading one byte at a time is extremely inefficient, you probably want something like: Reader.ReadByte Commented Apr 30, 2020 at 16:26

1 Answer 1

8

You're reading the entire file as a string (not bytes) at once, then processing it rune-by-rune (not byte-by-byte). To literally read the file one byte at a time (which is very likely not what you want), you must do exactly that:

f, err := os.Open("path")
if err != nil {
    panic(err)
}

b := make([]byte, 1)

for {
    err := f.Read(b)
    if err != nil && !errors.Is(err, io.EOF) {
        fmt.Println(err)
        break
    }

    // process the one byte b[0]

    if err != nil {
        // end of file
        break
    }
}

However, what you more likely want is to read the file efficiently, and process it one byte at a time. For this, you should use a buffered reader:

f, err := os.Open("path")
if err != nil {
    panic(err)
}

br := bufio.NewReader(f)

// infinite loop
for {

    b,err := br.ReadByte()

    if err != nil && !errors.Is(err, io.EOF) {
        fmt.Println(err)
        break
    }

    // process the one byte b

    if err != nil {
        // end of file
        break
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This for-loop is not working: expected boolean or range expression, found assignment (missing parentheses around composite literal?)
for b, err := binFileReader.ReadByte(); err == nil; { fmt.Println(b) } Something like this is needed I think

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.