golang 读取一行

//读取一行
func myReadLine(paths string) error {
    //先获取到文件信息
    fileinfo, err := os.Stat(paths)
    if err != nil {
        return fmt.Errorf("get file info error")
    }
    //判断是否是目录
    if fileinfo.IsDir() {
        return fmt.Errorf("paths is dir")
    }
    f, err := os.Open(paths)
    rd := bufio.NewReader(f)
    for {
        //跟ReadString功能相似,不过是返回的字节切片
        /*
            lineBytes, err := rd.ReadBytes('\n')
            line := string(lineBytes)
        */
        line, err := rd.ReadString('\n')
            }
            lineBytes, err := rd.ReadBytes('\n')
            line := string(lineBytes)
        */
        line, err := rd.ReadString('\n')
        line = strings.Trim(line, "\n")
        fmt.Printf("%s\n", line)
        if err != nil || err == io.EOF {
            if err != nil {
                return fmt.Errorf("ReadString error")
            }
            break
        }
    }
    return nil
}

  

转载于:https://www.cnblogs.com/chengsheng/p/10628557.html

你可能感兴趣的:(golang 读取一行)