Method, interface and pointers in golang
• go, and golang
- If a method is defined on a pointer to struct, the method can be called on a pointer to the struct type. In the example below, calling
ptr_s1.hello()is fine since methodhello()is defined on*struct1.
type struct1 struct {
greeting string
}
func (s *struct1) hello() {
s.greeting = "hello"
}
func main() {
ptr_s1 := &struct1{}
fmt.Printf("ptr_s1 is of type : %+T\n", ptr_s1) // *main.struct1
fmt.Printf("ptr_s1.greeting: %+v\n", ptr_s1) // &{greeting:}
ptr_s1.hello()
fmt.Printf("ptr_s1.greeting: %+v\n", ptr_s1) // &{greeting:hello}
s1 := struct1{}
fmt.Printf("s1 is of type : %+T\n", s1) // main.struct1
fmt.Printf("s1.greeting: %+v\n", s1) // {greeting:}
s1.hello()
fmt.Printf("s1.greeting: %+v\n", s1) // {greeting:hello}
}- However, method
hello()is also callable on non pointer types1. In fact,struct1.greetinggets populated withhello.