-1

the code

package main

import "fmt"

type unimplementedGreeterServer struct {
}

func (unimplementedGreeterServer) SayHello() string {
    return "hello"
}

func main() {
    s := &unimplementedGreeterServer{}
    ret := s.SayHello()
    fmt.Println(ret)
}

the result

hello

the question : why the SayHello method has no unimplementedGreeterServer point or unimplementedGreeterServer receiver can run

I think the right will be

func (s unimplementedGreeterServer) SayHello2() string {
    return "hello"
}

func (s *unimplementedGreeterServer) SayHello3() string {
    return "hello"
}

not

func (unimplementedGreeterServer) SayHello() string {
    return "hello"
}
torek
  • 448,244
  • 59
  • 642
  • 775
fuyou001
  • 1,594
  • 4
  • 16
  • 27

1 Answers1

3

The receiver itself is optional. If the method does not use the receiver, you can omit it. The declaration:

func (unimplementedGreeterServer) SayHello() string {
    return "hello"
}

simply defines a method for unimplementedGreeterServer that does not use the receiver. It is defined for a value receiver, so it is defined for unimplementedGreeterServer and *unimplementedGreeterServer.

Burak Serdar
  • 46,455
  • 3
  • 40
  • 59