Struct के Tags
type Name struct {
Field type `key:"value"`
Other type `json:"other,omitempty"`
}
Writing a Struct Tag
एक struct tag किसी field के type के बाद रखा एक raw string है (backticks के बीच लिखा गया), conventionally space-separated key:"value" pairs रखते हुए जिन्हें encoding/json जैसी libraries reflection के ज़रिए पढ़ती हैं।
उदाहरण: Writing a Struct Tag
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define a struct type named Product
type Product struct {
Name string `json:"name"`
Price float64 `json:"price"`
}
// main is where the program starts running
func main() {
// Short declaration: create p and infer the type
p := Product{Name: "Notebook", Price: 3.5}
// Print using a format string
fmt.Printf("%+v\n", p)
}
Login to try C/C++/Java/PHP code in the editor
Using Tags with encoding/json
json tag बिल्कुल control करता है कि किसी struct को JSON में convert करते समय कौन सी key name उपयोग हो, आपको Go के exported-field naming convention (Name) का पालन करते हुए lowercase JSON keys ("name") produce करने देते हुए जो common API conventions से मेल खाती हैं।
उदाहरण: Using Tags with encoding/json
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"encoding/json"
"fmt"
)
// Define a struct type named Product
type Product struct {
Name string `json:"name"`
Price float64 `json:"price"`
}
// main is where the program starts running
func main() {
// Short declaration: create p and infer the type
p := Product{Name: "Notebook", Price: 3.5}
// Short declaration: create data, _ and infer the type
data, _ := json.Marshal(p)
// Print the values followed by a newline
fmt.Println(string(data))
}
Login to try C/C++/Java/PHP code in the editor
Omitting Empty Fields
किसी json tag में ',omitempty' जोड़ना encoder को यह बताता है कि जब वह field अपनी zero value रखे तो output में इसे पूरी तरह skip कर दे, जो कई fields optional होने पर generated JSON को compact रखता है।
उदाहरण: Omitting Empty Fields
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"encoding/json"
"fmt"
)
// Define a struct type named Product
type Product struct {
Name string `json:"name"`
Note string `json:"note,omitempty"`
}
// main is where the program starts running
func main() {
// Short declaration: create p and infer the type
p := Product{Name: "Pen"}
// Short declaration: create data, _ and infer the type
data, _ := json.Marshal(p)
// Print the values followed by a newline
fmt.Println(string(data))
}
Login to try C/C++/Java/PHP code in the editor
- backtick-quoted tag string syntax को गलत करना (अंदर quotes का mismatch), जिससे tag चुपचाप नज़रअंदाज़ हो जाता है बजाय error देने के।
- यह भूल जाना कि encoding/json को किसी struct field को बिल्कुल भी देखने के लिए वह exported (capitalized) होना चाहिए, इसके tag की परवाह किए बिना।
- json जैसी tag key को JSON spell करना, जिसे reflection-based libraries नहीं पहचानेंगी।
- एक struct tag किसी field से जुड़ा एक string literal है, इसके type के बाद backticks में लिखा गया।
- encoding/json marshaling के दौरान field name और behavior control करने के लिए
json:"name"tags पढ़ता है। - tags की परवाह किए बिना सिर्फ exported (capitalized) fields ही encoding/json को दिखाई देती हैं।
- Tags runtime पर reflection के ज़रिए पढ़े जाते हैं -- Go compiler field को कैसे treat करता है इस पर इनका कोई असर नहीं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: