Structs क्या हैं
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
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Struct Literals
एक struct value एक literal से बनाई जा सकती है जो named fields के लिए values list करे (किसी भी order में), या exact declaration order से मेल खाती positional values के साथ -- readability और safety के लिए named fields को जोरदार प्राथमिकता दी जाती है।
उदाहरण: Struct Literals
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)
}
Login to try C/C++/Java/PHP code in the editor
Modifying Struct Fields
एक बार आपके पास एक struct value हो, इसके fields dot notation से access और modify किए जाते हैं, बिल्कुल ज़्यादातर दूसरी languages में किसी object की property access करने जैसा।
उदाहरण: Modifying Struct Fields
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Comparing Structs
एक जैसे type की दो struct values == से compare की जा सकती हैं जब तक हर field खुद comparable हो -- comparison यह जांचता है कि सभी fields बराबर हैं, सिर्फ यह नहीं कि वे memory में एक ही object हैं।
उदाहरण: Comparing Structs
// 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)
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि दूसरे packages से accessible होने (exported) के लिए struct field names capitalized होने चाहिए, सिर्फ style के लिए नहीं।
- दो structs को == से compare करना जब उनमें से एक field slice या map जैसी non-comparable type हो, जो compile होने में fail होता है।
- 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: