首次提交:初始化项目代码
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user