file, _ := os.Open("x.txt")
f := bufio.NewReader(file)
for {
read_line, _ := ReadString('\n')
fmt.Print(read_line)
// other code what work with parsed line...
}
end it add \n on every line , end program to work , only work with last line...
Please put example, i try anything end any solution what i find here not work for me.
If you want to read a file line-by-line, using
bufio.Scanner
will be easier. Scanner won't includesendline
(\n
or\r\n
) into the line.If you want to remove
endline
, I suggest you to useTrimRightFunc
, i.e.Update:
As pointed by @GwynethLlewelyn, using
TrimRight
will be simpler (cleaner), i.e.Internally,
TrimRight
function callTrimRightFunc
, and will remove the character if it match any character given as the second argument ofTrimRight
.You can slice off the last character:
Perhaps a better approach is to use the strings library: