Methods with the Same Name
In the Go programming language, you can have two or more methods with the same name in the same package, but the receivers must be different types. This feature isn't accessible in Go functions, which means you can't write functions with the same name in the same package; the compiler will produce an error if you try.
Syntax
The general syntax for defining functions with the same name is as follows.
func(receiver *ReceiverType) MethodName(parameters) returnType {
//method body
}
func(receiver2 ReceiverType) MethodName(parameters) returnType{
//method body
}
We can see that though both methods have the same name, they have different receiver types.
Implementation
Now, we will see the implementation of methods with the same name in Golang. There are two methods with the same name in the below example, i.e., speak(). Both methods have different receivers of the struct type. Let’s look at the below code.
Code
//methods with same name as struct fields
package main
import "fmt"
type person struct {
first string
last string
age int
}
type animal struct {
name string
age int
}
func(a animal) speak() string {
return fmt.Sprintf("%s says woof", a.name)
}
func(p person) speak() string {
return fmt.Sprintf("%s %s says hello", p.first, p.last)
}
func main() {
p1 := person{
first: "James",
last: "Bond",
age: 32,
}
a1 := animal{
name: "Tiger",
age: 5,
}
fmt.Println(p1.speak())
fmt.Println(a1.speak())
}
Explanation
There are two structs defined in the above programme. A first struct is a person, and the second is an animal. Then we defined a function called "speak()" with a struct person as the receiver and another function with the same name but with the animal as the receiver. In the main function, we have provided the required values to different parameters of the structs. Then, we have invoked the speak() method.
Output
Frequently Asked Questions
Is Golang difficult to learn?
Because the syntax of Golang is similar to that of C, it is simple to learn, particularly for C-style or Java programmers. Go's syntax is smaller than that of many other programming languages, and it has a minimal set of features to get the job done.
Is Golang compiled or interpreted?
Golang is a compiler-based language that may be readily compiled on a development machine for any target operating system, including Linux and Mac. When a Golang project is built, it becomes a self-contained executable that can be run on the target machine without any other software.
Is it worthwhile to learn Go by the year 2022?
Thanks to its growing popularity among programmers, Golang is still worth learning in 2022. According to Stack Overflow's 2020 Developer Survey, Golang rose to fifth place on the most popular languages list from ninth place in 2019.
Is Go a strongly typed language?
Go is a statically typed, strongly typed language. Int, byte, and string are examples of primitive types. There are also structs to consider. The type system allows the compiler to catch entire classes of bugs, just like any other strongly typed language.
Conclusion
In this article, we have extensively discussed the basics of methods in Golang and methods having the same name in a package. We implemented the program for Go methods, having the same name but different receiver types.
We hope that this blog has helped you enhance your knowledge regarding “Methods with the same name in Go.” If you would like to learn more, check out our articles on “Go Loops,” “Defer in Go,” “Pointer to an Array as Function Argument in Go,” “Returning Pointer from a Function in Go,” and “Go Routines.” Do upvote our blog to help other ninjas grow.
Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations and much more!
Happy Reading!