Home > Backend Development > Golang > golang method manual

golang method manual

WBOY
Release: 2023-05-16 12:26:38
Original
1094 people have browsed it

Golang is a programming language that is widely loved by developers. It has won the favor of many developers for its efficiency, simplicity, and ease of learning and use. In Golang, a method is a function that allows operations on a struct type.

This article will introduce you to Golang methods, including details on method definition, calling, delivery, interfaces, etc., to help you better master the use of Golang methods.

1. Definition of method

In Golang, a method is a special type of function, which is associated with the structure type, that is, the receiver of the method must point to a certain structure type . The basic syntax of method definition is as follows:

func (r ReceiverType) methodName(parameterList) (resultList) {

// 方法体
Copy after login

}

Among them, ReceiverType refers to the method receiver Type, can be any type, including pointer type. If ReceiverType is a pointer type, it will be automatically dereferenced when calling the method; methodName refers to the name of the method; parameterList refers to the parameter list received by the method, if If there is no need to pass parameters, it can be omitted; resultList refers to the return value list of the method. If no return value is required, it can be omitted.

For example, we can define a method named Print, which receives a pointer to the Person type and does not need to pass parameters and return values:

type Person struct {

name string
age int
Copy after login

}

func (p *Person) Print() {

fmt.Printf("Name: %s, Age: %d
Copy after login

", p.name, p.age)
}

above In the example, the method Print belongs to the Person type, and it is bound to a pointer to the Person type, so the method can be called through an instance of the Person type.

2. Calling the method

In In Golang, method calls are very similar to function calls. You only need to add the receiver before the function name. For example, we can create an instance of the Person type and then call the Print method for output:

func main() {

p := &Person{
    name: "Tom",
    age:  18,
}
p.Print()
Copy after login

}

In the above example, we create a pointer p pointing to the Person type and call its Print method for output. It should be noted that the method call There is no need to pass the receiver explicitly, Golang will automatically convert the caller into the receiver.

3. Method transfer

In Golang, methods can also be passed like functions .For example, we can encapsulate the above Print method into a function:

func PrintPerson(p *Person) {

p.Print()
Copy after login

}

Then we can use the PrintPerson function as The parameters are passed to another function:

func ProcessPerson(f func(*Person)) {

p := &Person{
    name: "Tom",
    age:  18,
}
f(p)
Copy after login

}

Finally, we can call the ProcessPerson function and use the PrintPerson function as Parameters are passed in:

func main() {

ProcessPerson(PrintPerson)
Copy after login

}

In the above example, the ProcessPerson function receives a function as a parameter and an instance of the Person type Passed to the function as a parameter. Since the PrintPerson function receives a pointer to the Person type as a parameter and conforms to the function type, the PrintPerson function can be passed as a parameter to the ProcessPerson function and output is successful.

四, Method interface

Interface in Golang is a very important concept. It defines a collection of methods. Any type that implements these methods can be regarded as conforming to the interface. Therefore, in Golang, interfaces can be implemented using methods.

For example, we can create an interface named Stringer, which defines a method named String to convert the structure into a string:

type Stringer interface {

String() string
Copy after login

}

Then we can define a String method on the Person type to convert Person to a string:

func (p *Person) String() string {

return fmt.Sprintf("Name: %s, Age: %d", p.name, p.age)
Copy after login

}

Finally, we can implement the Stringer interface for the Person type and use the Println function in the fmt package for output:

func main() {

p := &Person{
    name: "Tom",
    age:  18,
}
fmt.Println(p)
Copy after login

}

In the above example, the Person type implements the Stringer interface and converts it into a string for output.

Summary

This article introduces the details of the definition, calling, delivery, interface, etc. of Golang methods. I hope it can help readers better understand the use of Golang methods. It should be noted that when using methods, you need to pay attention to the receiver type and method definition, as well as details such as the calling method and delivery method of the method. Only by mastering these details can we give full play to the advantages of Golang methods and improve the efficiency and maintainability of the code.

The above is the detailed content of golang method manual. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template