Function definition in this form?

I could not think of a better (more descriptive) title.

I am learning Go, and I sometimes see the following form for defining a function:

func (c *currency) GetRate (ctx context.Context, rr *protos.RateRequest) (*protos.RateResponse, error) {}

My question:
If this were instead

func GetRate (ctx context.Context, rr *protos.RateRequest) (*protos.RateResponse, error) {}

then I would understand it to be defining a function named “GetRate”, with a list of arguments and return.

What I cannot understand is the use of “func (c *currency)” followed by the name of the function, GetRate.

Can anyone please help me understand?

You can call this a special type of function, a method, which can operate on a specific type, in this case type is “currency”.
Here are some links for you:

  1. Defining Methods in Go | DigitalOcean
  2. A Tour of Go (methods)
2 Likes

@handsomefox,

Thank you!
I see now: defining a method on a type.

I am humbly grateful for your help with this question.

Be well.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.