企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 解析 csv ``` in := `first_name,last_name,username "Rob","Pike",rob Ken,Thompson,ken "Robert","Griesemer","gri" ` r := csv.NewReader(strings.NewReader(in)) //r.Comma = ';' // 指定分隔符 //r.Comment = '#' // 指定注释 for { record, err := r.Read() if err == io.EOF { break } if err != nil { log.Fatal(err) } fmt.Println(record) } // Output: // [first_name last_name username] // [Rob Pike rob] // [Ken Thompson ken] // [Robert Griesemer gri] ``` ## 数组转 csv ``` records := [][]string{ {"first_name", "last_name", "username"}, {"Rob", "Pike", "rob"}, {"Ken", "Thompson", "ken"}, {"Robert", "Griesemer", "gri"}, } w := csv.NewWriter(os.Stdout) w.WriteAll(records) // calls Flush internally if err := w.Error(); err != nil { log.Fatalln("error writing csv:", err) } ```