Home  >  Article  >  Backend Development  >  golang function method

golang function method

WBOY
WBOYOriginal
2023-05-16 12:08:39392browse

Golang is one of the most popular programming languages ​​currently. One of its characteristics is the excellent design of functions and methods. This article will introduce the relevant knowledge of Golang functions and methods in detail.

1. Function

Basic syntax of Golang function:

func function_name( [parameter list] ) [return_types] {
   //函数体
}

The parameter list of the function and its return value type are optional and do not need to be written. If the function has only one return value and its type is a non-interface type, the parentheses can be omitted.

For example:

func add(x int, y int) int {
   return x + y
}

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

The function add receives two parameters of type int, and the return value type is also int. The function is called through the expression add(1, 2) and its return value is output. The output is 3.

Golang functions support multiple return values. For example:

func swap(x, y string) (string, string) {
   return y, x
}

func main() {
   a, b := swap("hello", "world")
   fmt.Println(a, b)
}

The function swap receives two string type parameters, and the return value type is two string types, that is, the return value is two strings, through multiple assignment a, b := swap("hello" , "world") assign the function return values ​​to a and b respectively, and finally output hello world.

2. Method

In the Golang language, a method refers to a function on a certain type of receiver, which is a class. This type can be a struct or any custom type.

The definition format of the method is as follows:

func (variable_name type) function_name([parameter list]) [return_types] {
    //函数体  
}

Among them, variable_name is an instance of the type (type), and function_name is the method of the type.

For example:

type Rectangle struct {
   length, width int
}

func (r Rectangle) area() int {
   return r.length * r.width
}

func main() {
   r := Rectangle{length: 10, width: 5}
   fmt.Println("area: ", r.area())
}

The above code defines the type Rectangle, which has two fields: length and width, and also defines the method area(), so it is a class. In the method declaration, the receiver is of type Rectangle, the method is named area(), and the return value is of type int.

The following statement tests this class by creating a Rectangle instance named r and calling its area() method.

The execution output result is:

area: 50

The method can also have a pointer receiver, which is a pointer pointing to the type. For example:

type Circle struct {
   radius float64
}

func (c *Circle) area() float64 {
   return math.Pi * c.radius * c.radius
}

func main() {
   c := Circle{radius: 5}
   fmt.Println("area:", c.area())
}

The above code defines the type Circle, which has a field called radius. It also defines the method area() and points to the type through a pointer receiver. Therefore, the area() method will change the value on the Circle instance.

Finally, we need to note that all types in Golang can have methods unless they are explicitly marked as copy types (receicer). However, in some cases, especially in high-performance industries, it is more performance-efficient to use pointer receivers. However, you need to remember these rules because if you accidentally write the wrong method, it will cause errors in your program.

3. Summary

In the Golang language, functions and methods (classes) are the basic programming units. By learning the grammatical structure of functions, we can easily define and call functions, making the program structure clearer and more concise. At the same time, the method is built into the class and is part of the class, so it encapsulates the instance variables and methods in the class. In this way, we can manage various information and methods of the program more conveniently, and avoid the waste of resources.

The above is the detailed content of golang function method. 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
Previous article:golang advanced usageNext article:golang advanced usage