Friday, September 9, 2022

Function Name Convention in Golang

This recommendations are applicable for function and methods

Uses camelCase to define the names, example:

getSomthing()

DoSomething()

print()



The function/method name should describe what it does, you should not have a name and execute another action.

Example use:

func getSomeList() {

   return myList

}


If a function/method defines context as a parameter and receives other parameters, the context must be the first one.

Example use:

func doSomething(ctx context.Context, account userAccount) {

}


If your function/methods has a context as a parameter. You should use “ctx” as a name instead of myContext, myCtx, currentCtx, cContext, etc.

When you define a method, the receiver should use one, two, maximum of three letters from the struct name that implements the method.

Example, use:

type Service struct {}

func (s Service)doSomething(ctx context.Context, account userAccount) {

}



or use:

type Service struct {}

func (svc Service)doSomething(ctx context.Context, account userAccount) {

}


0 comments:

Post a Comment