To read until a specific delimiter or marker using bufio.Reader
in Go, you can use the ReadString()
method to read until the delimiter is encountered. Here's an example:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
file, err := os.Open("input.txt")
if err != nil {
fmt.Println("Error opening file:", err)
os.Exit(1)
}
defer file.Close()
reader := bufio.NewReader(file)
delimiter := "\n" // specify the delimiter
for {
line, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Error reading file:", err)
break
}
if line == delimiter {
// Do something when delimiter is encountered
break
}
// Process the line
fmt.Print(line)
}
}
In this example, a file named "input.txt" is being read line by line until a newline character (\n
) is encountered. You can replace "\n"
with any other delimiter or marker you require.