Table of contents
1.
Introduction
2.
Methods in Golang
2.1.
Syntax of Functions
2.2.
Syntax of Methods
3.
Methods with the Same Name
3.1.
Syntax
4.
Implementation
4.1.
Code
4.2.
Explanation
4.3.
Output
5.
Frequently Asked Questions
5.1.
Is Golang difficult to learn?
5.2.
Is Golang compiled or interpreted?
5.3.
Is it worthwhile to learn Go by the year 2022?
5.4.
Is Go a strongly typed language?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Methods with the Same Name in Go

Introduction

Google created Go, commonly known as Golang, as an open-source, compiled, and statically typed programming language. It's designed to be simple, fast, readable, and efficient. The Go standard library includes several built-in functions and methods that you can use in your program. Apart from this, a user can define their own required methods.

What if two user-defined methods have the same name in Golang?

This article will discuss how to create multiple functions with the same name and different concepts.

Methods in Golang

Before going to the concept of “methods” in Golang, we will have to understand functions in Golang. A function is a collection of statements that work together to accomplish a goal. It allows the user to reuse the same code, resulting in less memory usage. Every Go programme includes at least one function, i.e., main(). The name, return type, and function parameters are all specified in a function declaration. A function definition defines the body of the function.

The only difference between Go methods and Go functions is that the method has a receiver parameter. The method can access the receiver's properties due to the receiver parameter. The receiver can be either struct or non-struct in this case.

Also see - Difference between argument and parameter

Syntax of Functions

A function definition in the Go programming language has the following general syntax.

func functionName(parameters) returnTypes {
    //code block
}

The “func” keyword represents the declaration of a function. The “functionName” in the above syntax is the actual function name given by the user. "Parameters” refer to a function's type, order, and number of parameters. “Return types” is a list of data types for the values returned by the function.

Syntax of Methods

func(receiver_name Type) methodName(parameters) return_types {
    //code
}

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!

Live masterclass