Home  >  Article  >  Backend Development  >  golang method set

golang method set

WBOY
WBOYOriginal
2023-05-09 21:32:06628browse

Golang is a simple and powerful programming language that provides a rich method set for operating objects. A method set is a collection of methods that can be called on a value of a specific type.

Golang’s method set consists of the following types:

  1. Value type method set

Value type method set represents all values ​​belonging to this type collection of methods. Value types include basic types (int, float, string, etc.) and custom types (struct, array, slice, map, etc.). Taking struct as an example, all fields in the structure will be included in the value type method set. When a method is called on a struct, a copy of this value is made and operated on.

The following is an example:

type Person struct {
    Name string
    Age int
}

func (p Person) SayHello() {
    fmt.Println("Hello, my name is", p.Name, "and I am", p.Age, "years old.")
}

func main() {
    p := Person{Name: "John", Age: 30}
    p.SayHello() // 输出:Hello, my name is John and I am 30 years old.
}
  1. Pointer type method set

Pointer type method set represents the collection of all methods belonging to this type of pointer. Unlike value types, pointer types include all methods that have pointer receivers. The function of the pointer type method set is to operate on the original object.

The following is an example:

type Rect struct {
    Width int
    Height int
}

func (r *Rect) Area() int {
    return r.Width * r.Height
}

func main() {
    r := &Rect{Width: 10, Height: 5}
    fmt.Println(r.Area()) // 输出:50
}
  1. Combination of value type and pointer type method sets

When a type has both a value type method set and a pointer type method set, it has two different method sets. This type is often called the receiver type. Since this type supports both value and pointer receivers, it can be called on either a value or a pointer.

The following is an example:

type Counter struct {
    Value int
}

func (c Counter) Increment() {
    c.Value++
}

func (c *Counter) Decrement() {
    c.Value--
}

func main() {
    c1 := Counter{Value: 0}
    c1.Increment()
    fmt.Println(c1.Value) // 输出:0

    c2 := &Counter{Value: 0}
    c2.Decrement()
    fmt.Println(c2.Value) // 输出:-1
}

In the above example, you can add an increment method through the value type receiver and add a decrement method through the pointer type receiver. When the increment method is called on a value, only the copy of the variable is changed, not the original variable. When the decrement method is called on a pointer, the original variable will be modified.

In general, Golang's method set provides a simple and powerful way to deal with objects. It makes performing operations on objects easier and more intuitive, and effectively avoids duplication of code. For beginners, it is very important to understand the ideas behind method sets, which will help to better understand the basic principles of the Golang programming language.

The above is the detailed content of golang method set. 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