What is the method used to call in the Go language?

In Go language, the method call is done using the “.” symbol. To call a method, simply add the “.” symbol after the object or type, followed by the method name. Example:

package main

import "fmt"

type Person struct {

    name string

    age int

}

func (p Person) SayHello() {

    fmt.Printf("Hello, my name is %s. I am %d years old.\n", p.name, p.age)

}

func main() {

    p := Person{name: "Alice", age: 20}

    p.SayHello() // 调用Person类型的SayHello方法

}

In the example above, we defined a Person struct and a SayHello method. In the main function, we created a Person object p and called the SayHello method using p.SayHello().

bannerAds