Skip to content
เข้าสู่ระบบ

JSON

การทำงานกับ JSON ใน Go ด้วย encoding/json package

package main
import (
"encoding/json"
"fmt"
)
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
IsActive bool `json:"is_active"`
}
func main() {
user := User{
ID: 1,
Name: "Alice",
Email: "alice@example.com",
IsActive: true,
}
// Marshal to JSON
data, err := json.Marshal(user)
if err != nil {
panic(err)
}
fmt.Println(string(data))
// {"id":1,"name":"Alice","email":"alice@example.com","is_active":true}
}

เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม

ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด