package main
 
import (
    "encoding/csv"
    "fmt"
    "io"
    "log"
    "os"
)
 
func main() {
    //准备读取文件
    fileName := "D:\\gotest\\src\\source\\test.csv"
    fs, err := os.Open(fileName)
    iferr != nil {
        log.Fatalf("can not open the file, err is %+v", err)
    }
    defer fs.Close()
 
    r := csv.NewReader(fs)
    //针对大文件,一行一行的读取文件
    for{
        row, err := r.Read()
        iferr != nil && err != io.EOF {
            log.Fatalf("can not read, err is %+v", err)
        }
        iferr == io.EOF {
            break
        }
        fmt.Println(row)
    }
 
    //针对小文件,也可以一次性读取所有的文件
    //注意,r要重新赋值,因为readall是读取剩下的
    fs1, _ := os.Open(fileName)
    r1 := csv.NewReader(fs1)
    content, err := r1.ReadAll()
    iferr != nil {
        log.Fatalf("can not readall, err is %+v", err)
    }
    for_, row := range content {
        fmt.Println(row)
    }
 
}