Embedded Structs क्या हैं
type Outer struct {
Inner
Field type
}
o.InnerField // promoted field
Embedding a Struct
किसी struct type को उसे field name दिए बिना किसी दूसरे struct के अंदर रखना इसे embed करता है, और Go अपने आप इसकी fields को promote करता है ताकि उन्हें बिना अतिरिक्त dot के outer struct पर सीधे access किया जा सके।
उदाहरण: Embedding 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 Address
type Address struct {
City, Country string
}
// Define a struct type named Person
type Person struct {
Name string
Address
}
// main is where the program starts running
func main() {
// Short declaration: create p and infer the type
p := Person{Name: "Kiran", Address: Address{City: "Pune", Country: "India"}}
// Print the values followed by a newline
fmt.Println(p.Name, p.City, p.Country)
}
Login to try C/C++/Java/PHP code in the editor
Accessing the Embedded Struct Directly
fields promoted होने के बावजूद, embedded struct अब भी इसके type name के ज़रिए एक पूरी चीज़ के रूप में पहुंचने योग्य है, जो तब उपयोगी है जब आपको सिर्फ वह inner टुकड़ा किसी दूसरे function को पास करना हो।
उदाहरण: Accessing the Embedded Struct Directly
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define a struct type named Address
type Address struct {
City string
}
// Define a struct type named Person
type Person struct {
Name string
Address
}
// main is where the program starts running
func main() {
// Short declaration: create p and infer the type
p := Person{Name: "Meera", Address: Address{City: "Delhi"}}
// Print the values followed by a newline
fmt.Println(p.Address.City)
}
Login to try C/C++/Java/PHP code in the editor
Promoted Methods
अगर embedded type के पास methods हों, वे methods भी promote होते हैं, इसलिए outer struct के पास वे सीधे होते दिखते हैं -- यह types के बीच behavior share करने का Go का मुख्य mechanism है।
उदाहरण: Promoted Methods
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define a struct type named Engine
type Engine struct {
Horsepower int
}
// Define the method Describe on type Engine
func (e Engine) Describe() string {
// Sprintf builds a formatted string instead of printing it
return fmt.Sprintf("%d hp engine", e.Horsepower)
}
// Define a struct type named Car
type Car struct {
Engine
Model string
}
// main is where the program starts running
func main() {
// Short declaration: create c and infer the type
c := Car{Engine: Engine{Horsepower: 300}, Model: "Speedster"}
fmt.Println(c.Model, "-", c.Describe())
}
Login to try C/C++/Java/PHP code in the editor
- यह सोचना कि embedding Java/C++ में inheritance जैसा है -- यह composition है; अकेले embedded type के ज़रिए कोई polymorphism नहीं है।
- किसी regular field को embedded type के promoted field जैसा नाम देना, ambiguity पैदा करते हुए जिसे explicitly हल करना चाहिए।
- यह भूल जाना कि खुद embedded struct (सिर्फ इसकी promoted fields नहीं) access करने के लिए, आप type name को field name के रूप में उपयोग करते हैं।
- Embedding field को explicitly नाम दिए बिना एक struct type को दूसरे के अंदर रखता है।
- Embedded type की fields और methods promoted हैं -- सीधे outer struct पर accessible।
- Embedding composition है, inheritance नहीं -- अकेले embedding से कोई runtime polymorphism नहीं।
- Embedded struct खुद इसके type name के ज़रिए एक field के रूप में अब भी accessible है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: