The following is a detailed explanation of the difference between methods and functions in Go from the golang tutorial column. 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.
# 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.
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 *Person
type The isAdult
method is declared.
Function calls use independent parameters, and method calls use the receiver type.
p := NewPerson("John", 21) fmt.Println(p.isAdult()) // true
Functions and methods are theoretically interchangeable. For example, we can convert the isAdult
method into a function and the NewPerson
function 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.
Let’s look at some common use cases encountered in Go applications, and the appropriate abstraction (function or method) for each application:
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 of Person
using 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")
In the Interchangeability example, we saw the use of a PersonFactory
object 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 like NewPerson
for 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 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 of isAdult
customer := NewPerson("John", 21) // Method customer.isAdult() // Function isAdult(customer)
Here customer.isAdult()
For asking "whether the customer is an adult ?" is much better understood than isAdult(customer)
. Furthermore, when you ask "Is x an adult?" it's always asked in the context of x.
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!