Methods क्या हैं
In this page:
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
// 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())
}
Login to try C/C++/Java/PHP code in the editor
Methods vs Functions
एक method असल में receiver को अपने पहले argument के रूप में लेने वाले function के लिए सिर्फ syntactic sugar है -- फर्क सिर्फ इसमें है कि इसे कैसे call किया जाता है (dot notation) और यह कि इसे सिर्फ उसी package में types से जोड़ा जा सकता है।
उदाहरण: Methods vs Functions
// 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())
}
Login to try C/C++/Java/PHP code in the editor
Methods on Non-Struct Types
Methods सिर्फ structs तक सीमित नहीं हैं -- आप अपने package में declared किसी भी named type पर एक method define कर सकते हैं, int या string जैसी किसी साधारण type पर आधारित एक सहित।
उदाहरण: Methods on Non-Struct Types
// 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())
}
Login to try C/C++/Java/PHP code in the editor
- किसी method को receiver type के गलत position में define करना, यह भूलते हुए कि यह func और method name के बीच जाता है।
- किसी दूसरे package में declared type पर एक method define करने की कोशिश करना, जिसकी Go अनुमति नहीं देता -- आप सिर्फ अपने owned types में methods जोड़ सकते हैं।
- किसी 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: