search
HomeBackend DevelopmentGolangWhat is the difference between methods and functions in go language

Difference: 1. Function is a piece of code with independent functions, which can be called repeatedly many times to achieve code reuse; while method is the behavioral function of a class and can only be called by objects of that class. . 2. Methods have receivers, but functions have no receivers. 3. Functions cannot have the same name, but methods can have the same name. 4. The calling methods are different. 5. The method needs to specify its type, which can be a structure or a custom type, while functions are universal. 6. The form and parameter type of the function must be consistent, and the method can be changed.

What is the difference between methods and functions in go language

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

In the Go language, functions and methods are different and have clear conceptual distinctions. In other languages, such as Java, generally speaking, a function is a method, and a method is a function. However, in the Go language, a function refers to a method that does not belong to any structure or type. In other words, a function has no receiver; Methods have receivers. The methods we are talking about either belong to a structure or belong to a newly defined type.

Function

Function and method, although the concepts are different, the definitions are very similar. The function definition statement has no receiver, so we can define the statement directly in the go file and under the go package.

func main() {
    sum := add(1, 2)
    fmt.Println(sum)
}

func add(a, b int) int {
    return a + b
}

In the example, we defined add as a function, its function signature is func add(a, b int) int, there is no receiver, directly It is defined under a package of go and can be called directly. For example, the main function in the example calls the add function.

The function name in the example starts with lowercase add, so its scope only belongs to the declared package and cannot be used by other packages. If we change the function name to Starting with a capital letter, the scope of the function is large and can be called by other packages. This is also the use of upper and lower case in the Go language. For example, in Java, there are special keywords to declare the scope private, protect, public, etc.

/*
 提供的常用库,有一些常用的方法,方便使用
*/
package lib

// 一个加法实现
// 返回a+b的值
func Add(a, b int) int {
    return a + b
}

The Add method defined in the above example can be called by other packages.

Method

The declaration of method is similar to that of function. The difference is that when the method is defined, it will be placed between func and the method name. Add a parameter, this parameter is the receiver, so that the method we define is bound to the receiver, and is called the method of the receiver.

type person struct {
    name string
}

func (p person) String() string{
    return "the person name is "+p.name
}

In the example, pay attention to the parameter (p person) added between func and the method name. This is the receiver. Now we say that type person has a String method, now let's see how to use it.

func main() {
    p:=person{name:"张三"}
    fmt.Println(p.String())
}

The calling method is very simple. Just use the type variable to call. The type variable and method are preceded by a . operator, which indicates that a certain method of this type variable is to be called. mean.

There are two types of receivers in the Go language: value receivers and pointer receivers. In our above example, we are using a value type receiver.

Using the method defined by the value type receiver, when called, actually uses a copy of the value receiver, so any operation on the value will not affect the original type variable.

func main() {
    p:=person{name:"张三"}
    p.modify() //值接收者,修改无效
    fmt.Println(p.String())
}

type person struct {
    name string
}

func (p person) String() string{
    return "the person name is "+p.name
}

func (p person) modify(){
    p.name = "李四"
}

In the above example, the printed value is still 张三, and modifications to it are invalid. If we use a pointer as the receiver, it will work, because the pointer receiver passes a copy of the pointer to the original value. The copy of the pointer still points to the value of the original type, so when it is modified, it will also Affects the value of the original type variable.

func main() {
    p:=person{name:"张三"}
    p.modify() //指针接收者,修改有效
    fmt.Println(p.String())
}

type person struct {
    name string
}

func (p person) String() string{
    return "the person name is "+p.name
}

func (p *person) modify(){
    p.name = "李四"
}

You only need to change it and become the receiver of the pointer, and the modification is completed.

When calling a method, the receivers passed are essentially copies, but one is a copy of the value, and the other is a copy of the pointer pointing to the value. The pointer has the characteristic of pointing to the original value, so modifying the value pointed by the pointer also modifies the original value. We can simply understand that the value receiver uses a copy of the value to call the method, while the pointer receiver uses the actual value to call the method.

In the above example, did you find that when we call the pointer receiver method, we also use a value variable, not a pointer. It is also possible if we use the following .

p:=person{name:"张三"}
(&p).modify() //指针接收者,修改有效

This is also possible. If we do not force the use of pointers for calls, the Go compiler will automatically help us get the pointers to meet the receiver's requirements.

Similarly, if it is a method of a value receiver, it can also be called using a pointer. The Go compiler will automatically dereference to meet the requirements of the receiver, such as String( defined in the example ) method can also be called like this:

p:=person{name:"张三"}
fmt.Println((&p).String())

In short, when calling a method, you can use either a value or a pointer. We don’t need to strictly abide by these. The Go language compiler will help us. Automatically escaped, which greatly facilitates us developers.

不管是使用值接收者,还是指针接收者,一定要搞清楚类型的本质:对类型进行操作的时候,是要改变当前值,还是要创建一个新值进行返回?这些就可以决定我们是采用值传递,还是指针传递。

Go语言中方法和函数的区别

1、含义不同

函数function是一段具有独立功能的代码,可以被反复多次调用,从而实现代码复用。而方法method是一个类的行为功能,只有该类的对象才能调用。

2、方法有接受者,而函数无接受者

  • Go语言的方法method是一种作用于特定类型变量的函数,这种特定类型变量叫做Receiver(接受者、接收者、接收器); 

  • 接受者的概念类似于传统面向对象语言中的this或self关键字;

  • Go语言的接受者强调了方法具有作用对象,而函数没有作用对象; 

  • 一个方法就是一个包含了接受者的函数; 

  • Go语言中, 接受者的类型可以是任何类型,不仅仅是结构体, 也可以是struct类型外的其他任何类型。

3、函数不可以重名,而方法可以重名

只要接受者不同,则方法名可以一样。

4、调用方式不一样

方法是struct对象通过.点号+名称来调用,而函数是直接使用名称来调用。

方法的调用需要指定类型变量调用,函数则不需要

 ```
var p testMethod.Person
p.Name = "tom"
p.Age = 123
p.ShowInfo()

注:方法和函数的访问权限都受大小写影响,小写本包,大写全局

5、方法需要指定所属类型,可以是结构体也可以是自定义type,函数则通用

```
func (person Person) ShowInfo(形参) 返回值{

   person.Name = "123"
   person.Age = 12
   fmt.Printf("name=%v,age=%v",person.Name,person.Age)
}
```
 
person为类型的形参,类型为Person

6、函数的形参与传参类型需要一致,方法可以改变

     ```
func (person *Person) ShowInfo(形参) 返回值{

   person.Name = "123"
   person.Age = 12
   fmt.Printf("name=%v,age=%v",person.Name,person.Age)
}

(1)这里方法的类型形参为指针,调用时可以使用 p.ShowInfo()或者 (&p).ShowInfo(),本质上都是后者,只不过Go的设计者对于方法的调用做了底层优化

func (person Person) ShowInfo(形参) 返回值{

   person.Name = "123"
   person.Age = 12
   fmt.Printf("name=%v,age=%v",person.Name,person.Age)
}

(2)这里方法的形参类型为数值型,默认为值传递,而在调用时可以使用p.ShowInfo()或者 (&p).ShowInfo(),但依旧是值拷贝

func test01(i *int) {


}

(3)对于函数则需保持一致,需要的形参为指针,则传入的形参需为地址值,否则编译无法通过

【相关推荐:Go视频教程编程教学

The above is the detailed content of What is the difference between methods and functions in go language. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Choosing Between Golang and Python: The Right Fit for Your ProjectChoosing Between Golang and Python: The Right Fit for Your ProjectApr 19, 2025 am 12:21 AM

Golangisidealforperformance-criticalapplicationsandconcurrentprogramming,whilePythonexcelsindatascience,rapidprototyping,andversatility.1)Forhigh-performanceneeds,chooseGolangduetoitsefficiencyandconcurrencyfeatures.2)Fordata-drivenprojects,Pythonisp

Golang: Concurrency and Performance in ActionGolang: Concurrency and Performance in ActionApr 19, 2025 am 12:20 AM

Golang achieves efficient concurrency through goroutine and channel: 1.goroutine is a lightweight thread, started with the go keyword; 2.channel is used for secure communication between goroutines to avoid race conditions; 3. The usage example shows basic and advanced usage; 4. Common errors include deadlocks and data competition, which can be detected by gorun-race; 5. Performance optimization suggests reducing the use of channel, reasonably setting the number of goroutines, and using sync.Pool to manage memory.

Golang vs. Python: Which Language Should You Learn?Golang vs. Python: Which Language Should You Learn?Apr 19, 2025 am 12:20 AM

Golang is more suitable for system programming and high concurrency applications, while Python is more suitable for data science and rapid development. 1) Golang is developed by Google, statically typing, emphasizing simplicity and efficiency, and is suitable for high concurrency scenarios. 2) Python is created by Guidovan Rossum, dynamically typed, concise syntax, wide application, suitable for beginners and data processing.

Golang vs. Python: Performance and ScalabilityGolang vs. Python: Performance and ScalabilityApr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Golang vs. Other Languages: A ComparisonGolang vs. Other Languages: A ComparisonApr 19, 2025 am 12:11 AM

Go language has unique advantages in concurrent programming, performance, learning curve, etc.: 1. Concurrent programming is realized through goroutine and channel, which is lightweight and efficient. 2. The compilation speed is fast and the operation performance is close to that of C language. 3. The grammar is concise, the learning curve is smooth, and the ecosystem is rich.

Golang and Python: Understanding the DifferencesGolang and Python: Understanding the DifferencesApr 18, 2025 am 12:21 AM

The main differences between Golang and Python are concurrency models, type systems, performance and execution speed. 1. Golang uses the CSP model, which is suitable for high concurrent tasks; Python relies on multi-threading and GIL, which is suitable for I/O-intensive tasks. 2. Golang is a static type, and Python is a dynamic type. 3. Golang compiled language execution speed is fast, and Python interpreted language development is fast.

Golang vs. C  : Assessing the Speed DifferenceGolang vs. C : Assessing the Speed DifferenceApr 18, 2025 am 12:20 AM

Golang is usually slower than C, but Golang has more advantages in concurrent programming and development efficiency: 1) Golang's garbage collection and concurrency model makes it perform well in high concurrency scenarios; 2) C obtains higher performance through manual memory management and hardware optimization, but has higher development complexity.

Golang: A Key Language for Cloud Computing and DevOpsGolang: A Key Language for Cloud Computing and DevOpsApr 18, 2025 am 12:18 AM

Golang is widely used in cloud computing and DevOps, and its advantages lie in simplicity, efficiency and concurrent programming capabilities. 1) In cloud computing, Golang efficiently handles concurrent requests through goroutine and channel mechanisms. 2) In DevOps, Golang's fast compilation and cross-platform features make it the first choice for automation tools.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment