Home >Backend Development >Golang >How golang defines methods for structure types

How golang defines methods for structure types

PHPz
PHPzOriginal
2023-04-05 09:09:45848browse

In the Go language, a structure is a user-defined data type, which consists of a set of member fields of various types. Methods can be defined to perform operations on the structure. The types of methods and functions in Go language are very similar, but methods and functions are different. Methods are functions defined in a structure type. In this article, we will explain how to define methods for struct types.

1. What is Golang’s method?

In the Go language, a method is a function that operates on this type of data. A method has access to all public fields and methods of the type it is bound to. Their syntax is similar to functions. The only difference is that in their signatures, the method name is preceded by a receiver, which is the structure type to which this method belongs.

For example:

type Rectangle struct {
    width, height int
}

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

A structure type "Rectangle" is defined here, and a method "area" is defined for it. This method returns an integer value by calculating the area of ​​the rectangle. Note the "(r Rectangle)" in the function signature. This is a receiver that allows this method to be called on type "Rectangle".

In the code to implement the method, we need to define a receiver. The receiver can be a value type or a pointer type. Value type receivers use a copy of the value as the method's receiver, and pointer type receivers use a pointer to the value as the method's receiver. For pointer type receivers, the method can modify the value itself or call other methods to modify it. For value type receivers, these modifications are ignored.

2. How to define methods for types?

The following is a sample program that demonstrates how to define methods for structure types.

package main

import "fmt"

type Rectangle struct {
    width, height int
}

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

func (r Rectangle) perimeter() int {
    return 2 * (r.width + r.height)
}

func main() {
    r := Rectangle{10, 5}
    fmt.Println("Area: ", r.area())
    fmt.Println("Perimeter: ", r.perimeter())
}

In this program, we define a structure type named "Rectangle". We define two methods for this type: "area" and "perimeter".

The "area" method returns the area of ​​the rectangle, and the "perimeter" method returns the perimeter of the rectangle.

In the "main" function, we create an instance of the Rectangle type named "r". We get the area and perimeter of the rectangle by calling the "r.area()" and "r.perimeter()" methods and print the values ​​to the console.

3. Using pointer type receivers in types

Now suppose we want to modify the properties of the Rectangle type. We need to use pointer types in the receiver, not value types. This way we can modify the fields in the instance.

type Rectangle struct {
    width, height int
}

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

func (r *Rectangle) perimeter() int {
    return 2 * (r.width + r.height)
}

func (r *Rectangle) resize(newWidth int, newHeight int) {
    r.width = newWidth
    r.height = newHeight
}

func main() {
    r := &Rectangle{10, 5}
    fmt.Println("Area: ", r.area())
    fmt.Println("Perimeter: ", r.perimeter())
    r.resize(12, 6)
    fmt.Println("Area: ", r.area())
    fmt.Println("Perimeter: ", r.perimeter())
}

In the above code, we have the pointer type "*Rectangle" as the receiver. This means that we can modify the value of the type in the method implementation. We define a new method "resize" to pass the values ​​of the new width and height and assign them to the width and height properties of the Rectangle.

In the main function, we create a pointer to the Rectangle instance, "r". We then call the method we defined earlier and pass the new height and width values ​​to the "resize" method. Finally, we print the new area and perimeter of the rectangle to the console.

4. Summary

In Go language, methods are functions defined in structure types. These methods have access to all public fields and methods of the type to which they are bound. In the code to implement the method, we need to define a receiver. The receiver can be a value type or a pointer type. Value type receivers use a copy of the value as the method's receiver, and pointer type receivers use a pointer to the value as the method's receiver. For pointer type receivers, the method can modify the value itself or call other methods to modify it. For value type receivers, these modifications are ignored.

The above is the detailed content of How golang defines methods for structure types. 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:Can golang be used?Next article:Can golang be used?