VOOZH about

URL: https://www.geeksforgeeks.org/go-language/methods-with-same-name-in-golang/

⇱ Methods With Same Name in Golang - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Methods With Same Name in Golang

Last Updated : 26 Aug, 2019
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: 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: Output:
Result 1: GeeksforGeeks
Result 2: 532
Comment
Article Tags:

Explore