为什么在声明此函数时将其分为三个部分?

 收藏

我正在尝试了解如何在golang中测试AWS调用,并且无法了解此功能的情况。

func (m mockedReceiveMsgs) ReceiveMessageRequest(in *sqs.ReceiveMessageInput) sqs.ReceiveMessageRequest {
    // Only need to return mocked response output
    return sqs.ReceiveMessageRequest{
        Request: &aws.Request{
            Data:        &m.Resp,
            HTTPRequest: &http.Request{},
            Retryer:     aws.NoOpRetryer{},
        },
    }
}

我了解这是一个称为RecieveMessageRequest的函数,该函数接受指向RecieveMessage int的指针并输出一个RecieveMessageRequest

But what is this section called? (m mockedReceiveMsgs)

我不明白为什么不将其作为普通参数传递。

回复
  • ReceiveMessageRequest is a method of the type mockedReceiveMsgs, and m is the receiver. If you have a variable x of type mockedReceiveMsgs, then you can call this as x.ReceiveMessageRequest(arg), and the m in the method body will be a copy of x.

  • You should read about struct here.

    (m mockedReceiveMsgs) that means ReceiveMessageRequest is a method of mockedReceiveMsgs struct.

    您可以在OOP中理解相同的方法