40 lines
702 B
Go
40 lines
702 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
// Product 大数据模型
|
|
type Product struct {
|
|
SKU string `json:"sku"`
|
|
Price float64 `json:"price"`
|
|
}
|
|
|
|
func main() {
|
|
// 流式写入
|
|
file, _ := os.Create("json/bulma_products.json")
|
|
defer file.Close()
|
|
enc := json.NewEncoder(file)
|
|
for i := 1; i <= 5000; i++ {
|
|
_ = enc.Encode(Product{
|
|
SKU: fmt.Sprintf("BULMA%05d", i),
|
|
Price: 99.9,
|
|
})
|
|
}
|
|
fmt.Println("\n流式写入完成")
|
|
|
|
// 流式读取
|
|
rf, _ := os.Open("json/bulma_products.json")
|
|
defer rf.Close()
|
|
dec := json.NewDecoder(rf)
|
|
count := 0
|
|
for dec.More() {
|
|
var p Product
|
|
_ = dec.Decode(&p)
|
|
count++
|
|
}
|
|
fmt.Printf("流式解析完成,总数:%d\n", count)
|
|
}
|