golang function method

WBOY
Release: 2023-05-05 21:51:07
Original
525 people have browsed it

Golang is a high-performance, strongly typed, fast-running programming language. It supports object-oriented programming ideas and has a very flexible way of defining functions and methods. This article will introduce in detail the definition, parameter passing, calling and common implementation methods of functions and methods in Golang.

1. Definition and use of functions

It is very simple to define functions in Golang. Its basic syntax is as follows:

func 函数名(参数1 类型1, 参数2 类型2, …) 返回类型 {
    //函数体代码
    return 返回值
}
Copy after login

Among them, function name is The name of the function, parametersare the input values ​​of the function, return typeis the type of the function’s output value. In the function body code, we can do whatever we want and use the return keyword to return the value we wish to return.

For example, the following is a function used to calculate the sum of two numbers:

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

The function call is also very simple, just provide the function name and parameters:

result := add(1, 2) //输出结果为3
Copy after login

2. Definition and use of methods

Methods in Golang are very similar to functions, but they are associated with a certain type or variable. Methods are typically used to modify or return properties of a structure, interface, or other type. The basic syntax of a method is as follows:

func (t 类型) 方法名(参数 参数类型) 返回类型 {
    //方法体代码
    return 返回值
}
Copy after login

where (t type) indicates that the method is associated with type t. In the method body code, we can access the properties and methods of the type through t. Similar to functions, methods can also define a receiver, that is, the instance to which the method will be attached.

For example, here is a rectangle type named Rect, which has an Area method for calculating the area of ​​a rectangle:

type Rect struct {
    width, height int
}

func (r Rect) Area() int {
    return r.width * r.height
}
Copy after login

In this example, we define a rectangle type named Rect, which has a width and height property. We also define a method called Area that uses the properties of the rectangle to calculate the area. Since we use the rectangle type as the receiver, we can call the Area method through its instance:

r := Rect{width: 10, height: 5}
result := r.Area()    //输出结果为50
Copy after login

3. Use of variable parameters

In Golang, you can Use ... (three dots) to define variadic parameters. This means that a function or method can take any number of parameters. For example, here is a function that contains variadic parameters:

func sum(nums ...int) int {
    result := 0
    for _, num := range nums {
        result += num
    }
    return result
}
Copy after login

This function accepts a variadic parameter named nums, which is of type int. We can call this function by passing any number of integers to this parameter:

result1 := sum(1, 2, 3, 4) //输出结果为10
result2 := sum(10, 20, 30) //输出结果为60
Copy after login

4. The use of closures

A closure in Golang means that a function can access the definition outside it The variable still exists even after the function returns. This is because functions in Golang are first-class functions and can be passed and used like variables. For example, here is a simple closure example:

func intSeq() func() int {
    i := 0
    return func() int {
        i++
        return i
    }
}

nextInt := intSeq()
fmt.Println(nextInt()) //输出结果为1
fmt.Println(nextInt()) //输出结果为2
Copy after login

In this example, we create a function that returns a function intSeq. The function defines a variable i, which is part of the closure. The returned function is also a closure, which can access i and increment it. We get a function from intSeq, call it nextInt, and use it to get two integers.

5. Use of interfaces

In Golang, an interface is a declaration of a set of methods, which define the behavior of some objects. Unlike programming languages ​​such as Java and C#, interfaces in Golang are implicit: if a type defines all the methods in the interface, the type automatically implements the interface without the need for explicit implementsstatement. For example, here is an example of an interface and an implementation of the interface:

type Shape interface {
    Area() float64
    Perimeter() float64
}

type Rect struct {
    width float64
    height float64
}

func (r Rect) Area() float64 {
    return r.width * r.height
}

func (r Rect) Perimeter() float64 {
    return 2 * (r.width + r.height)
}
Copy after login

In this example, we define an interface named Shape and define two methods for it: Area and Perimeter. We also defined a rectangle type named Rect and implemented two methods of the Shape interface. This means that the Rect type automatically implements the Shape interface.

Finally, we can create a variable of type Shape interface and assign it a value of type Rect. In this case, we can call the Area and Perimeter methods of the Shape interface:

var s Shape
s = Rect{width: 10, height: 5}
fmt.Println(s.Area())       //输出结果为50
fmt.Println(s.Perimeter())  //输出结果为30 
Copy after login

In summary, in Golang Functions and methods have very flexible definitions and implementations. We can write more powerful and flexible programs by using features such as variadic arguments, closures, and interfaces.

The above is the detailed content of golang function method. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!