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

Methods क्या हैं

एक method एक specific type से जुड़ा एक function है, जैसे हर Car को उसका अपना built-in Honk action देना जो वह perform कर सके।
Syntax
go
func (receiver Type) methodName(param type) returnType {
    // body
}

Defining a Method

एक method एक regular function जैसा दिखता है लेकिन method name से पहले एक अतिरिक्त receiver parameter रखता है, जो इसे एक specific type के साथ जोड़ता है -- इसे call करने के लिए उस type की एक value पर dot notation उपयोग होता है, जैसे p.Greet()।

उदाहरण: Defining a Method

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
}

// Define the method Greet on type Person
func (p Person) Greet() string {
	// Send "Hello, " + p.Name back to the caller
	return "Hello, " + p.Name
}

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

Methods vs Functions

एक method असल में receiver को अपने पहले argument के रूप में लेने वाले function के लिए सिर्फ syntactic sugar है -- फर्क सिर्फ इसमें है कि इसे कैसे call किया जाता है (dot notation) और यह कि इसे सिर्फ उसी package में types से जोड़ा जा सकता है।

उदाहरण: Methods vs Functions

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 Rectangle
type Rectangle struct {
	Width, Height float64
}

// Define the method Area on type Rectangle
func (r Rectangle) Area() float64 {
	// Send r.Width * r.Height back to the caller
	return r.Width * r.Height
}

// main is where the program starts running
func main() {
	// Short declaration: create r and infer the type
	r := Rectangle{Width: 4, Height: 5}
	// Print the values followed by a newline
	fmt.Println("area:", r.Area())
}

Methods on Non-Struct Types

Methods सिर्फ structs तक सीमित नहीं हैं -- आप अपने package में declared किसी भी named type पर एक method define कर सकते हैं, int या string जैसी किसी साधारण type पर आधारित एक सहित।

Note: यह pattern साधारण values में domain-specific behavior जोड़ने के लिए आम है, जैसे किसी custom type को validate करना।

उदाहरण: Methods on Non-Struct Types

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

type Celsius float64

// Define the method ToFahrenheit on type Celsius
func (c Celsius) ToFahrenheit() float64 {
	// Send float64(c)*9/5 + 32 back to the caller
	return float64(c)*9/5 + 32
}

// main is where the program starts running
func main() {
	// Short declaration: create temp and infer the type
	temp := Celsius(25)
	// Print the values followed by a newline
	fmt.Println(temp.ToFahrenheit())
}
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. किसी method को receiver type के गलत position में define करना, यह भूलते हुए कि यह func और method name के बीच जाता है।
  2. किसी दूसरे package में declared type पर एक method define करने की कोशिश करना, जिसकी Go अनुमति नहीं देता -- आप सिर्फ अपने owned types में methods जोड़ सकते हैं।
  3. किसी method (receiver के ज़रिए एक specific type से बंधा) को एक साधारण standalone function के साथ confuse करना।
चैप्टर सारांश
  • एक method एक special receiver argument वाला एक function है, जो इसे एक specific type से बांधता है।
  • Receiver func और method name के बीच parentheses में दिखाई देता है।
  • Methods सिर्फ उसी package में declared types पर define किए जा सकते हैं।
  • Methods वह building block हैं जो Go class methods की जगह data को behavior जोड़ने के लिए उपयोग करता है।
🔒

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.