f, err := os.Open("test.txt")if err != nil {panic(err)}defer f.Close()rd := bufio.NewReader(f)for {line, err := rd.ReadString('\n') //以'\n'为结束符读入一行if err != nil || io.EOF == err {if line==""{break}}fmt.Println(line)}
或者
f, err := os.Open("test.txt")if err != nil {panic(err)}defer f.Close()rd := bufio.NewReader(f)for {line, err := rd.ReadString('\n') //以'\n'为结束符读入一行fmt.Println(line)if err != nil || io.EOF == err {break} }