mirror of
https://github.com/Estom/notes.git
synced 2026-05-03 08:22:42 +08:00
go知识重新整理
This commit is contained in:
36
Go/gopkg/encoding/csv.go
Normal file
36
Go/gopkg/encoding/csv.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// Comma-Separated Values
|
||||
// 逗号分割值文件
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
// 单行读取
|
||||
r := strings.NewReader("a,b,c")
|
||||
csvR := csv.NewReader(r)
|
||||
records, _ := csvR.Read()
|
||||
fmt.Println(records) // [a b c]
|
||||
|
||||
// 全部读取
|
||||
f, _ := os.Open("demo.csv")
|
||||
csvR = csv.NewReader(f)
|
||||
fmt.Println(csvR.ReadAll()) // [[...]]
|
||||
|
||||
// 单行写入
|
||||
w := csv.NewWriter(os.Stdout)
|
||||
_ = w.Write(records)
|
||||
w.Flush() // a,b,c // 类 Flush() 函数将缓冲区的数据写入真实文件,勿忘
|
||||
|
||||
// 批量写入
|
||||
strs := [][]string{{"x", "y", "z"}, {"X", "Y", "Z"}}
|
||||
w.WriteAll(strs)
|
||||
w.Flush()
|
||||
// x,y,z
|
||||
// X,Y,Z
|
||||
}
|
||||
Reference in New Issue
Block a user