Golang permite criar dois ou mais métodos com o mesmo nome no mesmo pacote, mas os receptores desses métodos devem ser de tipos diferentes. Esse recurso não está disponível em funções Go, o que significa que você não tem permissão para criar métodos com o mesmo nome no mesmo pacote. Se você tentar fazer isso, o compilador apresentará um erro.

Sintaxe:
func(reciver_name_1 Type) method_name(parameter_list)(return_type){
// Code
}
func(reciver_name_2 Type) method_name(parameter_list)(return_type){
// Code
}
Vejamos o exemplo a seguir para entender melhor os métodos com o mesmo nome em Golang:
Exemplo 1:
// Chương trình Go minh họa cách
// tạo các phương thức cùng tên
package main
import "fmt"
// Tạo các cấu trúc
type student struct {
name string
branch string
}
type teacher struct {
language string
marks int
}
// Các phương thức cùng tên nhưng với
// kiểu receiver khác nhau
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)
}
// Hàm chính
func main() {
// Khởi tạo các giá trị
// of the structures
val1 := student{"Rohit", "EEE"}
val2 := teacher{"Java", 50}
// Gọi các phương thức
val1.show()
val2.show()
}
Resultado:
Name of the Student: Rohit
Branch: EEE
Language: Java
Student Marks: 50
Explicação: No exemplo acima, temos dois métodos com o mesmo nome, ou seja, show() , mas com tipos de recebimento diferentes. Aqui, o primeiro método show() contém s do tipo student e o segundo método show() contém t do tipo teacher . E na função main() , chamamos ambos os métodos com a ajuda de suas respectivas variáveis de estrutura. Se você tentar criar esses métodos show() com o mesmo tipo de receptor, o compilador gerará um erro.
Exemplo 2:
// Chương trình Go minh họa cách
// tạo các phương thức cùng tên
// với receiver không phải struct
package main
import "fmt"
type value_1 string
type value_2 int
// Tạo hàm cùng tên với
// các kiểu receiver không phải struct khác nhau
func (a value_1) display() value_1 {
return a + "forGeeks"
}
func (p value_2) display() value_2 {
return p + 298
}
// Hàm chính
func main() {
// Khởi tạo giá trị này
res1 := value_1("Geeks")
res2 := value_2(234)
// Hiện kết quả
fmt.Println("Result 1: ", res1.display())
fmt.Println("Result 2: ", res2.display())
}
Resultado:
Result 1: GeeksforGeeks
Result 2: 532