GoLang, what is contents in parenthesis before MethodName?

前端 未结 1 795
南笙
南笙 2021-01-26 23:15
func (t *T) MethodName(argType T1, replyType *T2) error

what is contents in parenthesis before MethodName? I mean this (t *T)

This comes from

相关标签:
1条回答
  • 2021-01-26 23:44

    The Go Programming Language Specification

    Method declarations

    A method is a function with a receiver. A method declaration binds an identifier, the method name, to a method, and associates the method with the receiver's base type.

    Given type Point, the declarations

    func (p *Point) Length() float64 {
        return math.Sqrt(p.x * p.x + p.y * p.y)
    }
    
    func (p *Point) Scale(factor float64) {
        p.x *= factor
        p.y *= factor
    }
    

    bind the methods Length and Scale, with receiver type *Point, to the base type.

    It's the method receiver.

    0 讨论(0)
提交回复
热议问题