- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Methods in Golang
In Go, methods are functions that are associated with a specific type. They allow developers to define behavior for objects of that type. In this article, we will explore the basics of methods in Go, how to define and use them, and the different types of methods available.
Defining Methods in Golang
In Go, methods are defined by associating them with a type using the following syntax −
func (receiver type) methodName(parameters) returnType { // method body }
The "receiver" is the type that the method is associated with. It can be a value or a pointer. The "methodName" is the name of the method. The "parameters" are the input arguments to the method, and the "returnType" is the output type of the method.
Here is an example of a method associated with a type −
type Rectangle struct { length float64 width float64 } func (r Rectangle) Area() float64 { return r.length * r.width }
In this example, we define a Rectangle struct and associate an Area() method with it. The method calculates the area of the rectangle using the length and width properties of the struct.
Using Methods in Golang
Methods in Go are called using the dot notation. For example −
r := Rectangle{length: 10.0, width: 5.0} area := r.Area()
In this example, we create a Rectangle struct with a length of 10.0 and a width of 5.0. We then call the Area() method on the struct to calculate its area.
Types of Methods in Golang
There are two types of methods in Go: value receiver methods and pointer receiver methods.
Value Receiver Methods
Value receiver methods are methods that receive a copy of the value of the receiver. They do not modify the original value of the receiver.
Example
Here is an example −
package main import ( "fmt" "math" ) type Circle struct { radius float64 } func (c Circle) Area() float64 { return math.Pi * c.radius * c.radius } func main() { c := Circle{radius: 5.0} fmt.Println("Circle Area:", c.Area()) }
Output
Circle Area: 78.53981633974483
In this example, we define a Circle struct and associate an Area() method with it. The method receives a copy of the Circle struct and calculates its area using the radius property.
Pointer Receiver Methods
Pointer receiver methods are methods that receive a pointer to the value of the receiver. They modify the original value of the receiver.
Example
Here is an example −
package main import "fmt" type Square struct { side float64 } func (s *Square) Scale(factor float64) { s.side = s.side * factor } func main() { sq := Square{side: 5.0} fmt.Println("Original Square Side:", sq.side) sq.Scale(2) fmt.Println("Scaled Square Side:", sq.side) }
Output
Original Square Side: 5 Scaled Square Side: 10
In this example, we define a Square struct and associate a Scale() method with it. The method receives a pointer to the Square struct and scales its side property by the factor passed as an argument.
Conclusion
Methods in Go allow developers to define behavior for objects of a specific type. By associating methods with a type, developers can create more intuitive and expressive code. There are two types of methods in Go: value receiver methods and pointer receiver methods. Value receiver methods receive a copy of the value of the receiver, while pointer receiver methods receive a pointer to the value of the receiver. By using methods in Go, developers can create more efficient and flexible code.