← Back to Go Course | Chapter 6: Structs & Methods | Lesson 6 of 6

Struct के Tags

एक struct tag किसी field से जुड़ा एक छोटा note है, जैसे एक sticky label जो दूसरे tools (जैसे JSON encoder) को ठीक-ठीक बताता है कि save या भेजते समय उस field को क्या कहा जाए।
Syntax
go
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

markup
// 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)
}

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 से मेल खाती हैं।

Note: सिर्फ capitalized (exported) fields ही encoding/json को कभी दिखाई देती हैं।

उदाहरण: Using Tags with encoding/json

markup
// 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))
}

Omitting Empty Fields

किसी json tag में ',omitempty' जोड़ना encoder को यह बताता है कि जब वह field अपनी zero value रखे तो output में इसे पूरी तरह skip कर दे, जो कई fields optional होने पर generated JSON को compact रखता है।

उदाहरण: Omitting Empty Fields

markup
// 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))
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. backtick-quoted tag string syntax को गलत करना (अंदर quotes का mismatch), जिससे tag चुपचाप नज़रअंदाज़ हो जाता है बजाय error देने के।
  2. यह भूल जाना कि encoding/json को किसी struct field को बिल्कुल भी देखने के लिए वह exported (capitalized) होना चाहिए, इसके tag की परवाह किए बिना।
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.