Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Types in Go
  • Go Keywords
  • Go Control Flow
  • Go Functions
  • GoLang Structures
  • GoLang Arrays
  • GoLang Strings
  • GoLang Pointers
  • GoLang Interface
  • GoLang Concurrency
Open In App
Next Article:
Methods With Same Name in Golang
Next article icon

Methods With Same Name in Golang

Last Updated : 26 Aug, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
In Go language, it is allowed to create two or more methods with the same name in the same package, but the receiver of these methods must be of different types. This feature does not available in Go function, means you are not allowed to create same name methods in the same package, if you try to do, then the compiler will throw an error. Syntax:
func(reciver_name_1 Type) method_name(parameter_list)(return_type){ // Code } func(reciver_name_2 Type) method_name(parameter_list)(return_type){ // Code }
Let us discuss this concept with the help of the examples: Example 1: C
// Go program to illustrate how to // create methods of the same name package main  import "fmt"  // Creating structures type student struct {     name   string     branch string }  type teacher struct {     language string     marks    int }  // Same name methods, but with // different type of receivers func (s student) show() {      fmt.Println("Name of the Student:", s.name)     fmt.Println("Branch: ", s.branch) }  func (t teacher) show() {      fmt.Println("Language:", t.language)     fmt.Println("Student Marks: ", t.marks) }  // Main function func main() {      // Initializing values     // of the structures     val1 := student{"Rohit", "EEE"}      val2 := teacher{"Java", 50}      // Calling the methods     val1.show()     val2.show() } 
Output:
  Name of the Student: Rohit  Branch:  EEE  Language: Java  Student Marks:  50  
Explanation: In the above example, we have two same name methods, i.e, show() but with different type of receivers. Here first show() method contain s receiver which is of the student type and the second show() method contains t receiver which is of the teacher type. And in the main() function, we call both the methods with the help of their respective structure variables. If you try to create this show() methods with the same type of receiver, then the compiler throws an error. Example 2: C
// Go program to illustrate how to // create the same name methods // with non-struct type receivers package main  import "fmt"  type value_1 string type value_2 int  // Creating same name function with // different types of non-struct receivers func (a value_1) display() value_1 {      return a + "forGeeks" }  func (p value_2) display() value_2 {      return p + 298 }  // Main function func main() {      // Initializing the values     res1 := value_1("Geeks")     res2 := value_2(234)      // Display the results     fmt.Println("Result 1: ", res1.display())     fmt.Println("Result 2: ", res2.display()) } 
Output:
  Result 1:  GeeksforGeeks  Result 2: 532  

Next Article
Methods With Same Name in Golang

A

ankita_saini
Improve
Article Tags :
  • Go Language
  • Go-Functions
  • Golang

Similar Reads

    Methods in Golang
    Go methods are like functions but with a key difference: they have a receiver argument, which allows access to the receiver's properties. The receiver can be a struct or non-struct type, but both must be in the same package. Methods cannot be created for types defined in other packages, including bu
    3 min read
    Promoted Methods in Golang Structure
    In Go structure, the working of promoted methods is just like Promoted Fields. We use this concept in the nested structure where a structure is a field in another structure, simply by just adding the name of the structure into another structure and it behaves like the Anonymous Field to the nested s
    3 min read
    reflect.MethodByName() Function in Golang with Examples
    Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.MethodByName() Function in Golang is used to get function value corresponding to the method of v with the given n
    2 min read
    Named Return Parameters in Golang
    Prerequisite: Functions in GolangIn Golang, Named Return Parameters are generally termed as the named parameters. Golang allows giving the names to the return or result parameters of the functions in the function signature or definition. Or you can say it is the explicit naming of the return variabl
    4 min read
    reflect.NumMethod() Function in Golang with Examples
    Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.NumMethod() Function in Golang is used to get the number of exported methods in the value's method set. To access
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences