SoFunction
Updated on 2025-03-05

Explain the details of the difference between Go functions and methods

Basic knowledge

Before understanding the differences between the two, I would like to briefly review the basic grammar knowledge. In the following example, define a function and method, and then call the function and method.

package main
import "fmt"
// Functions and methodsfunc function1() {
	("I'm a function called function1")
}
type User struct {
	name string
	age  uint
}
func (u User) function1() {
	("I'm a method called function1")
}
func main() {
	// Call function	function1()
	// Call method	User{}.function1()
}

Execute the above code,go run main, the following results will be printed:

kert@kertdeMBP ~/c/p/w/l/g/b/demo1 (master)> go run
I'm a function called function1
I'm a method called function1

Through the above example code, we can draw a few conclusions.

1. When calling a function, just use the function name (if the caller and the callee are both under the same package name); when calling a method, you need toInstantiationStructure, and then the method is called in the form of structure (there are many kinds of structure instantiation, and the sample code uses literal methods).

2. When defining the function, use it directlyfunc + function name()Yes; when defining the method, you need to use itfunc (receiver of method) + function name()Only do it.

3. The method is called in the form of ".", while the function uses the function name directly.

Use pointers

Next, use pointer masters to modify the effect of the value in functions and methods.

package main
import "fmt"
// Functions and methodsfunc function1(a *int) {
	*a = 19
}
type User struct {
	name string
	age  uint
}
func (u *User) function1() {
	 = 10
}
func main() {
	a := 10
	function1(&a)
	("Function function1 runs as", a)
	user := User{age: 12, name: "Zhang San"}
	user.function1()
	("The result of structure user is", )
}

Run the appeal code to see the execution effect.go run main

kert@kertdeMBP ~/c/p/w/l/g/b/demo1 (master)> go run
Function function1 runs as 19
The result of the structure user is 10

In the above code, by passing a pointer to the function, modify the value of the formal parameter a; receive a receiver of pointer type through the method. Both can modify the original value, and neither function nor method is the same.

Is the same name

Next, the following example code shows whether functions and methods support defining the same name.

package main
import "fmt"
// Functions and methodsfunc function1(a *int) {
	*a = 19
}
func function1() {
	("Print something casually")
}
type User struct {
	name string
	age  uint
}
func (u *User) function1() {
	 = 10
}
type Animal struct {
	name string
	age  uint
}
func (u *Animal) function1() {
	 = 10
}

Still running the appeal code and check how the execution results are.go run main

kert@kertdeMBP ~/c/p/w/l/g/b/demo1 (master)> go run
# command-line-arguments
./:10:6: function1 redeclared in this block
    ./:6:6: other declaration of function1

How to draw conclusions through the results of the operation.

1. The name of the function cannot be repeated. Regardless of whether the passed parameters or returned parameters are the same, the function name is not allowed to be repeatedly defined.

2. The name of the method can be repeated, provided that the recipients of the method cannot be the same.

Summarize

Different meanings

1. Function function is a piece of code with unique functions that can be called repeatedly to achieve code reuse. The method method is a behavioral function of a class, and only objects of that class can be called.

The method has a receiver, but the function has no receiver

1. Go method method is a function that acts on a specific type of variable. This type of variable is called Receiver (recipient, receiver, receiver); 2. The concept of the recipient is similar to the this or self keyword in the traditional object-oriented language; 3. Go recipient emphasizes that a method has a function that has a function, but a function has no object; 4. A method is a function that contains the recipient; 5. In Go, the type of the recipient can be any type, not just a structure, but any other type other than the struct type.

Functions cannot be renamed, but methods can be renamed.

1. As long as the recipients are different, the method names can be the same.

Different calls

1. The method is that the struct object is called through the .dot + name, while the function is called directly using the name.

This is the end of this article about the details of the difference between Go functions and methods. For more relevant content on the differences between Go functions and methods, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!