I am currently reading a text file input.txt
with the following inputs:
123
456
789
The code to parse it is:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
file, err := os.Open("input.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
count := 0
var line string
for scanner.Scan() {
count += 1
line = scanner.Text()
fmt.Println(line)
if line == "123" {
fmt.Println("EQUAL")
}
}
}
Why does the first line of the file not match the hard coded string 123
in the code?
As mentioned in the comments, this is due to special characters in the file. In this case the utf8 bom, but could be dos format , or other non-printable characters.