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

Structs क्या हैं

एक struct कई labeled blanks वाले एक form जैसा है -- एक shape एक नाम, एक उम्र, और एक address सब एक साथ bundled एक चीज़ के रूप में रख सकता है।
Syntax
go
type Name struct {
    Field1 type1
    Field2 type2
}

value := Name{Field1: v1, Field2: v2}

Defining a Struct

एक struct type 'type Name struct { }' से declared होता है, हर field का नाम और type list करते हुए। यह Go का कई संबंधित data को एक custom type में bundle करने का तरीका है।

उदाहरण: Defining a Struct

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 Person
type Person struct {
	Name string
	Age  int
}

// main is where the program starts running
func main() {
	// Short declaration: create p and infer the type
	p := Person{Name: "Asha", Age: 32}
	// Print the values followed by a newline
	fmt.Println(p.Name, p.Age)
}

Struct Literals

एक struct value एक literal से बनाई जा सकती है जो named fields के लिए values list करे (किसी भी order में), या exact declaration order से मेल खाती positional values के साथ -- readability और safety के लिए named fields को जोरदार प्राथमिकता दी जाती है।

Note: struct literals में named fields को प्राथमिकता दें -- वे self-documenting हैं और field reordering से बचे रहते हैं।

उदाहरण: Struct Literals

markup
package main

import "fmt"

type Point struct {
	X, Y int
}

func main() {
	a := Point{X: 3, Y: 4}
	b := Point{5, 6} // positional: X=5, Y=6
	fmt.Println(a, b)
}

Modifying Struct Fields

एक बार आपके पास एक struct value हो, इसके fields dot notation से access और modify किए जाते हैं, बिल्कुल ज़्यादातर दूसरी languages में किसी object की property access करने जैसा।

उदाहरण: Modifying Struct Fields

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 Counter
type Counter struct {
	Value int
}

// main is where the program starts running
func main() {
	// Short declaration: create c and infer the type
	c := Counter{Value: 0}
	c.Value = c.Value + 1
	c.Value++
	fmt.Println(c.Value)
}

Comparing Structs

एक जैसे type की दो struct values == से compare की जा सकती हैं जब तक हर field खुद comparable हो -- comparison यह जांचता है कि सभी fields बराबर हैं, सिर्फ यह नहीं कि वे memory में एक ही object हैं।

उदाहरण: Comparing Structs

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 Point
type Point struct {
	X, Y int
}

// main is where the program starts running
func main() {
	// Short declaration: create a and infer the type
	a := Point{1, 2}
	// Short declaration: create b and infer the type
	b := Point{1, 2}
	// Print the values followed by a newline
	fmt.Println("equal:", a == b)
}
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. यह भूल जाना कि दूसरे packages से accessible होने (exported) के लिए struct field names capitalized होने चाहिए, सिर्फ style के लिए नहीं।
  2. दो structs को == से compare करना जब उनमें से एक field slice या map जैसी non-comparable type हो, जो compile होने में fail होता है।
  3. positional values के साथ एक struct literal बनाना और field order गलत करना, बजाय स्पष्टता के लिए named fields उपयोग करने के।
चैप्टर सारांश
  • एक struct संबंधित fields को एक single named type में group करता है।
  • Struct literals named fields (recommended) या declaration order से मेल खाती positional values उपयोग कर सकते हैं।
  • Structs field by field == से compare होते हैं, जब तक हर field type खुद comparable हो।
  • Structs वह foundation हैं जो Go data को group करने के लिए classes के बजाय उपयोग करता है।
🔒

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.