How to read a single byte from a bufio.Reader in Go?

To read a single byte from a bufio.Reader in Go, you can use the ReadByte() function. Here's an example:

package main import ( "bufio" "fmt" "os" ) func main() { file, err := os.Open("example.txt") if err != nil { fmt.Println("Error opening file:", err) return } defer file.Close() reader := bufio.NewReader(file) // Read a single byte b, err := reader.ReadByte() if err != nil { fmt.Println("Error reading byte:", err) return } fmt.Printf("Byte read: %c\n", b) }

In this example, we open a file "example.txt" and create a bufio.Reader to read from it. We then use the ReadByte() function to read a single byte from the file. The ReadByte() function returns a byte and an error. If there is an error, we print an error message and return. Otherwise, we print the byte read using the fmt.Printf() function.