go 读写csv文件
package main import ( "encoding/csv" "fmt" "os" ) func write() { f, err := os.Create("test.csv") if err != nil{ fmt.Println(err) return } defer f.Close() var data = make([][]string, 4) data[0] = []string{"标题", "作者", "时间"} data[1] = []string{"羊皮卷", "鲁迅", "2008"} data[2] = []string{"易筋经", "唐生", "665"} f.WriteString("\xEF\xBB\xBF") // 写入一个UTF-8 BOM w := csv.NewWriter(f) //创建一个新的写入文件流 w.WriteAll(data) w.Flush() } func Read() { f, err := os.Open("test.csv") if err != nil{ fmt.Println(err) return } defer f.Close() w := csv.NewReader(f) data, err := w.ReadAll() if err != nil{ fmt.Println(err) return } fmt.Println(data) } func main() { Read() }