首次提交:初始化项目代码
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// User 基础数据模型
|
||||
type User struct {
|
||||
ID int64 `json:"user_id"`
|
||||
Username string `json:"username"`
|
||||
Age int `json:"age"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
// 序列化:Go → JSON
|
||||
user := User{ID: 1001, Username: "bulma_admin", Age: 26}
|
||||
jsonData, err := json.MarshalIndent(user, "", " ")
|
||||
if err != nil {
|
||||
fmt.Printf("序列化失败:%v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("=== 序列化结果 ===")
|
||||
fmt.Println(string(jsonData))
|
||||
|
||||
// 反序列化:JSON → Go
|
||||
jsonStr := `{"user_id":1002,"username":"json_test","age":28}`
|
||||
var newUser User
|
||||
if err := json.Unmarshal([]byte(jsonStr), &newUser); err != nil {
|
||||
fmt.Printf("反序列化失败:%v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("\n=== 反序列化结果 ===")
|
||||
fmt.Printf("%+v\n", newUser)
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
fmt "fmt"
|
||||
)
|
||||
|
||||
// BaseModel 公共基础模型
|
||||
type BaseModel struct {
|
||||
ID int64 `json:"id,string,omitempty"`
|
||||
CreatedAt string `json:"created_at,omitempty"`
|
||||
}
|
||||
|
||||
// Order 订单模型(全标签演示)
|
||||
type Order struct {
|
||||
BaseModel `json:",inline"`
|
||||
OrderNo string `json:"order_no"`
|
||||
Status int `json:"status,omitempty"`
|
||||
Phone int64 `json:"phone,string,omitempty"`
|
||||
Password string `json:"-"`
|
||||
Extra struct {
|
||||
Level int `json:"level"`
|
||||
} `json:",nosquash"`
|
||||
Ignore string `json:",allowunknown"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
order := Order{
|
||||
BaseModel: BaseModel{ID: 202606001, CreatedAt: "2026-06-02"},
|
||||
OrderNo: "BULMA-ORDER-001",
|
||||
Password: "secret_123",
|
||||
}
|
||||
data, _ := json.MarshalIndent(order, "", " ")
|
||||
fmt.Println("\n=== 高级标签运行结果 ===")
|
||||
fmt.Println(string(data))
|
||||
|
||||
// 演示 allowunknown 的反序列化功能
|
||||
fmt.Println("\n=== 反序列化测试(含未知字段)===")
|
||||
jsonWithUnknown := `{
|
||||
"id": "202606002",
|
||||
"created_at": "2026-06-02",
|
||||
"order_no": "BULMA-ORDER-002",
|
||||
"unknown_field": "test",
|
||||
"extra_field": 123
|
||||
}`
|
||||
var newOrder Order
|
||||
err := json.Unmarshal([]byte(jsonWithUnknown), &newOrder)
|
||||
if err != nil {
|
||||
fmt.Printf("反序列化失败:%v\n", err)
|
||||
} else {
|
||||
fmt.Printf("成功解析:%+v\n", newOrder)
|
||||
}
|
||||
|
||||
// 演示 omitempty 的效果
|
||||
fmt.Println("\n=== omitempty 效果对比 ===")
|
||||
emptyOrder := Order{
|
||||
BaseModel: BaseModel{ID: 0}, // ID 为 0,会被 omit
|
||||
OrderNo: "BULMA-ORDER-003",
|
||||
}
|
||||
data2, _ := json.MarshalIndent(emptyOrder, "", " ")
|
||||
fmt.Println(string(data2))
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// CustomTime 自定义时间类型
|
||||
type CustomTime time.Time
|
||||
|
||||
const timeLayout = "2006-01-02 15:04:05"
|
||||
|
||||
func (t CustomTime) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(time.Time(t).Format(timeLayout))
|
||||
}
|
||||
|
||||
func (t *CustomTime) UnmarshalJSON(data []byte) error {
|
||||
var timeStr string
|
||||
if err := json.Unmarshal(data, &timeStr); err != nil {
|
||||
return err
|
||||
}
|
||||
val, err := time.Parse(timeLayout, timeStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*t = CustomTime(val)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Mobile 手机号脱敏
|
||||
type Mobile string
|
||||
|
||||
func (m Mobile) MarshalJSON() ([]byte, error) {
|
||||
num := string(m)
|
||||
if len(num) == 11 {
|
||||
return json.Marshal(num[:3] + "****" + num[7:])
|
||||
}
|
||||
return json.Marshal(num)
|
||||
}
|
||||
|
||||
// UserDTO 数据传输对象
|
||||
type UserDTO struct {
|
||||
Name string `json:"name"`
|
||||
Phone Mobile `json:"phone"`
|
||||
CreateAt CustomTime `json:"create_at"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
dto := UserDTO{
|
||||
Name: "bulma",
|
||||
Phone: Mobile("10012345678"),
|
||||
CreateAt: CustomTime(time.Now()),
|
||||
}
|
||||
data, _ := json.MarshalIndent(dto, "", " ")
|
||||
fmt.Println("\n=== 自定义序列化结果 ===")
|
||||
fmt.Println(string(data))
|
||||
}
|
||||
@@ -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