JSON-কে encoding/json ট্যাগসহ Go struct ডেফিনিশনে কনভার্ট করুন। ফিল্ডের নাম Go-র exported-name নিয়ম আর golint-এর initialism তালিকা (ID, URL, API, ...) মেনে চলে, আর কলামগুলো এমনভাবে সাজানো থাকে যেন কোডটি gofmt দিয়ে চালানো হয়েছে।
Webhook payload বা API response json.Unmarshal করার জন্য একটি type, net/http, Gin বা Echo handler-এর request-body struct, অথবা কোনো CLI-এর config struct দরকার হলে এটি কাজে আসে।
omitempty ট্যাগ পায়।Struct-এর নাম-এ top-level struct-এর নাম দিন (ডিফল্ট Root); এটি URL-এ ?name= হিসেবে থাকে।.go ফাইলে পেস্ট করে package clause যোগ করুন। Time ফিল্ড পাওয়া গেলে import "time" যুক্ত থাকে।var ev WebhookEvent; err := json.Unmarshal(body, &ev) হিসেবে ব্যবহার করুন।{
"event_id": "evt_1Pq",
"type": "invoice.paid",
"created_at": "2024-05-01T03:15:00Z",
"amount_cents": 4999,
"customer": { "id": "cus_9", "email": "kim@example.com" },
"retry_count": null
}import "time"
type WebhookEvent struct {
EventID string `json:"event_id"`
Type string `json:"type"`
CreatedAt time.Time `json:"created_at"`
AmountCents int `json:"amount_cents"`
Customer Customer `json:"customer"`
RetryCount any `json:"retry_count,omitempty"`
}
type Customer struct {
ID string `json:"id"`
Email string `json:"email"`
}
golint-এর initialism নিয়মে event_id হয় EventID, আর RFC 3339 স্ট্রিং created_at-কে time.Time হিসেবে অনুমান করা হয়, যা import "time" যোগ করে। retry_count হলো null, তাই এর type অজানা: এটি omitempty-সহ any হয়।
json:"originalKey" ট্যাগ, আর কিছু row-এ না থাকা বা null থাকা ফিল্ডে ,omitempty।int, string, bool, struct, time.Time) pointer (*int) হয়, যাতে "অনুপস্থিত" আর zero value আলাদা থাকে। Slice আর any আগে থেকেই nil হতে পারে, তাই non-pointer থাকে।int, তার বেশি হলে int64, দশমিক থাকলে float64।2024-05-01T03:15:00Z-এর মতো RFC 3339 স্ট্রিংকে time.Time হিসেবে অনুমান করা হয়।user_id → UserID, home_url → HomeURL); সংখ্যা দিয়ে শুরু হওয়া key Field prefix পায়।"", সেগুলো বাদ দিয়ে struct-এর উপরে একটি কমেন্টে তালিকা করা হয়।[]Root-এ unmarshal করার একটি কমেন্ট তৈরি হয়।পেমেন্ট বা Git হোস্টিং সার্ভিসের পাঠানো একটি নমুনা ইভেন্ট থেকে struct তৈরি করে json.NewDecoder(r.Body).Decode(&ev) দিয়ে decode করুন, map[string]any-এর উপর বারবার type assertion না করে।
ক্লায়েন্টের একটি request নমুনা থেকে c.ShouldBindJSON(&req)-এর জন্য struct তৈরি করুন, তারপর যেখানে দরকার binding:"required" ট্যাগ যোগ করুন।
config.json-কে একবার struct-এ বদলে নিন, তাহলে os.ReadFile-এর পরে একটিমাত্র json.Unmarshal দিয়েই পুরো config টাইপড ভ্যালু হিসেবে পেয়ে যাবেন।
time.Time অনুমান হয়; 2024-05-01 03:15:00 বা Unix timestamp-এর মতো ভ্যালু string বা int থাকে।json.Unmarshal ডিফল্টভাবে অজানা key উপেক্ষা করে এবং key case-insensitive ভাবে মেলায়। কড়া decoding চাইলে Decoder.DisallowUnknownFields() ব্যবহার করুন।int 64-বিট, কিন্তু int64 লেখা হয় শুধু যখন কোনো নমুনা ভ্যালু 32 বিট ছাড়িয়ে যায়। বাড়তে পারে এমন ID-র জন্য নিজে int64-এ বদলে নেওয়া বেশি নিরাপদ।null থাকা ভ্যালু any হয়; আসল type জানা হলে সেগুলো বদলে দিন।*int-এর মতো pointer?সাধারণ int ফিল্ড key না থাকলে 0 হয়, যা আসল 0 থেকে আলাদা করা যায় না। Key অনুপস্থিত বা null হলে pointer হয় nil, আর omitempty-এর সাথে মিলে আবার marshal করার সময় key-টি বাদ পড়ে।
encoding/json শুধু exported (বড় হাতের অক্ষরে শুরু) ফিল্ড পড়ে ও লেখে; ছোট হাতের ফিল্ড ট্যাগ থাকলেও উপেক্ষিত হয়। Go-তে নাম বড় হাতের অক্ষরেই থাকে আর JSON key যায় ট্যাগে।
retry_count-এর মতো একটি null ফিল্ড any হিসেবে এসেছে।নমুনায় শুধু null থাকলে type অনুমান করার কিছু থাকে না। আসল ভ্যালুসহ আরেকটি response একটি array-তে যোগ করে দুটো একসাথে কনভার্ট করুন, অথবা হাতে *int-এর মতো কিছুতে বদলে দিন।
হ্যাঁ। উপরের Go struct → JSON ট্যাবে struct পেস্ট করুন; এটি ট্যাগগুলো পড়ে উদাহরণ JSON তৈরি করে।