- 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
How to add a method to struct type in Golang?
In Golang, structs are an essential part of the language and are used to define complex data types. Often, you may find yourself in a situation where you need to add a method to a struct to perform some specific action. In this article, we will discuss how to add a method to a struct type in Golang and provide some examples to help you understand the concept.
Defining a Struct in Golang
Before we dive into adding methods to a struct, let's first understand how to define a struct in Golang. A struct is a composite data type that groups together zero or more values of different types. Here is an example of a simple struct in Golang −
type Person struct { Name string Age int }
In the above example, we define a Person struct that has two fields, Name and Age.
Adding a Method to a Struct in Golang
Adding a method to a struct in Golang is simple. We define the method outside of the struct definition and use the struct as the receiver for the method. The receiver is a parameter that provides access to the fields of the struct.
Here's an example of how to add a method to the Person struct we defined earlier −
type Person struct { Name string Age int } func (p Person) SayHello() string { return fmt.Sprintf("Hello, my name is %s and I am %d years old", p.Name, p.Age) }
In the above example, we define a method called SayHello() that takes a Person struct as the receiver. The method returns a string that includes the name and age of the person.
To call the method, we create a new Person struct and invoke the method using dot notation −
func main() { p := Person{Name: "John", Age: 30} fmt.Println(p.SayHello()) // Output: Hello, my name is John and I am 30 years old }
Example
package main import "fmt" type Person struct { Name string Age int } func (p Person) SayHello() string { return fmt.Sprintf("Hello, my name is %s and I am %d years old", p.Name, p.Age) } func main() { p := Person{Name: "John", Age: 30} fmt.Println(p.SayHello()) // Output: Hello, my name is John and I am 30 years old }
Output
Hello, my name is John and I am 30 years old
Here's another example of adding a method to a struct in Golang −
Example
package main import "fmt" type Rectangle struct { Width float64 Height float64 } func (r Rectangle) Area() float64 { return r.Width * r.Height } func main() { r := Rectangle{Width: 5, Height: 10} fmt.Println("Rectangle Area:", r.Area()) // Output: Rectangle Area: 50 }
Output
Rectangle Area: 50
Conclusion
In this article, we discussed how to add a method to a struct type in Golang. We learned that adding a method is as simple as defining a function with the struct as the receiver. We also provided an example to illustrate the concept. By using these techniques, you can add methods to structs in Golang to perform specific actions on your data types.