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

Embedded Structs क्या हैं

एक struct को दूसरे के अंदर embed करना एक form को दूसरे form के blanks की built-in copy देने जैसा है, ताकि outer form को अपने आप inner वाले के सभी fields भी मिल जाएं।
Syntax
go
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

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

Accessing the Embedded Struct Directly

fields promoted होने के बावजूद, embedded struct अब भी इसके type name के ज़रिए एक पूरी चीज़ के रूप में पहुंचने योग्य है, जो तब उपयोगी है जब आपको सिर्फ वह inner टुकड़ा किसी दूसरे function को पास करना हो।

उदाहरण: Accessing the Embedded Struct Directly

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

Promoted Methods

अगर embedded type के पास methods हों, वे methods भी promote होते हैं, इसलिए outer struct के पास वे सीधे होते दिखते हैं -- यह types के बीच behavior share करने का Go का मुख्य mechanism है।

Note: यह वह तरीका है जिससे Go classical inheritance के बिना types में code reuse achieve करता है।

उदाहरण: Promoted Methods

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 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())
}
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. यह सोचना कि embedding Java/C++ में inheritance जैसा है -- यह composition है; अकेले embedded type के ज़रिए कोई polymorphism नहीं है।
  2. किसी regular field को embedded type के promoted field जैसा नाम देना, ambiguity पैदा करते हुए जिसे explicitly हल करना चाहिए।
  3. यह भूल जाना कि खुद 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:

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.