go方法定义问题,请帮忙解析下面的代码是什么意思

type RowsAffected int64

func (RowsAffected) LastInsertId() (int64, error) {
return 0, errors.New("no LastInsertId available")
}

这里的func (RowsAffected)...,是什么意思?方法定义一般不都像下面这样定义的吗?


func (recv receiver_type) methodName(parameter_list) (return_value_list) { … }

func (_ receiver_type) methodName(parameter_list) (return_value_list) { … }

func (this receiver_type) methodName(parameter_list) (return_value_list) { … }

func (self receiver_type) methodName(parameter_list) (return_value_list) { … }
已邀请:

kodango

赞同来自: youmi

receiver 在方法里面用不到,所以名称可以省略。

silenceshell - ieevee.com

赞同来自: youmi

https://golang.org/ref/spec#Method_declarations


A non-blank receiver identifier must be unique in the method signature. If the receiver's value is not referenced inside the body of the method, its identifier may be omitted in the declaration. The same applies in general to parameters of functions and methods.

youmi

赞同来自:

谢谢,明白了。不用回复了

huhuyou2 - fish

赞同来自:

这种定义方法在上面场景下用呢?如果 method body里面都不使用这个type的receiver,干嘛要在method name前加这个receiver呢?

要回复问题请先登录注册