Go reads Unix format or Windows format files by line

Go reads Unix format or Windows format files by line

fp, err := os.Open("t.txt")
defer fp.Close()
if nil == err {
    lines := bufio.NewReader(fp)
    for {
        oldLine, err := lines.ReadString('\n')
		newLine := strings.Replace(oldLine, '\r', "", -1)
        if err != nil || io.EOF == err {
            break
        }
        fmt.Println(newLine)
    }
}

Comments

Popular posts from this blog

Python Receiving and parse JSON Data via UDP protocol

ubus lua client method and event registration code demo/example