Home>Article>Backend Development> Detailed explanation of the difference between methods and functions in Go

Detailed explanation of the difference between methods and functions in Go

藏色散人
藏色散人 forward
2020-12-11 15:31:40 2890browse

The following is a detailed explanation of the difference between methods and functions inGofrom thegolang tutorialcolumn. I hope it will be helpful to friends in need!

This article will introduce the main differences between functions and methods in Go, and how to best use them.

banner image

# Functions and methods are used extensively in Go to provide abstractions that make our programs easier to read and reason about. On the surface, functions and methods look similar, but there are some important semantic differences that can greatly affect the readability of your code.

Syntax

Declaration syntax

Declare a function by specifying the types of parameters, the return value, and the function body:

type Person struct { Name string Age int } // 这个函数返回一个新的对象`Person` func NewPerson(name string, age int) *Person { return &Person{ Name: name, Age: age, } }

On the other hand, by specifying additional "receiver" (which in OOP terms would be the "class" to which the method belongs) to declare the method:

// `Person`指针类型接收者的`isAdult方法 func (p *Person) isAdult() bool { return p.Age > 18 }

In the method declaration above, we are on the*Persontype TheisAdultmethod is declared.

Execution syntax

Function calls use independent parameters, and method calls use the receiver type.

p := NewPerson("John", 21) fmt.Println(p.isAdult()) // true

Interchangeability

Functions and methodsaretheoretically interchangeable. For example, we can convert theisAdultmethod into a function and theNewPersonfunction as a method:

type PersonFactory struct {} // NewPerson现在是PersonFactory结构的方法 func (p *PersonFactory) NewPerson(name string, age int) *Person { return &Person{ Name: name, Age: age, } } // 现在,isAdult是一个函数,在该函数中,我们将`Person`作为参数而不是接收者进行传递 func isAdult(p *Person) bool { return p.Age > 18 }

In this case, the execution syntax looks a little strange:

factory := &PersonFactory{} p := factory.NewPerson("John", 21) fmt.Println(isAdult(p)) // true

The code above looks much more complicated than it needs to be. This shows us that the difference between methods and functions is mainly a syntactic one and appropriate abstraction should be used depending on the scenario.

Use Cases

Let’s look at some common use cases encountered in Go applications, and the appropriate abstraction (function or method) for each application:

Method Chaining

A very useful feature of methods is the ability to chain them together while still keeping the code clean. Let's take the example of setting some properties ofPersonusing a link:

type Person struct { Name string Age int } func (p *Person) withName(name string) *Person { p.Name = name return p } func (p *Person) withAge(age int) *Person { p.Age = age return p } func main() { p := &Person{} p = p.withName("John").withAge(21) fmt.Println(*p) // {John 21} }

It would look really horrible if we used a function for the same thing:

p = withName(withAge(p, 18), "John")

Stateful vs. Stateless Execution

In the Interchangeability example, we saw the use of aPersonFactoryobject to create a new instance of Person. It turns out that this is an anti-pattern and should be avoided.

It is better to use a function likeNewPersonfor stateless execution.

"Stateless" here refers to any code that always returns the same output for the same input

The corollary is that if you find that a function reads and modifies many specific type, it should probably be defined as a method of that type.

Semantics

Semantics refers to the way the code is read. If you read the code aloud in spoken language, which would make more sense?

Let’s take a look at the function and method implementation ofisAdult

customer := NewPerson("John", 21) // Method customer.isAdult() // Function isAdult(customer)

Herecustomer.isAdult()For asking "whether the customer is an adult ?" is much better understood thanisAdult(customer). Furthermore, when you ask "Is x an adult?" it's always asked in the context ofx.

Conclusion

While we discussed some key differences and use cases for functions and methods in Go, there are always exceptions! It is important not to rely on any of these rules as fundamental principles.

Finally, the difference between functions and methods is how the resulting code is read. If you or your team think one approach reads better than another, then that's the right abstraction!

Original address: https://www.sohamkamani.com/golang/functions-vs-methods/

Translation address: https://learnku.com/go/t /52424

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

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete