Passing parameters

It might so happen that you need to travel a long way through a stack of function calls. A very tempting solution would be to use a context to store that value and recall it only in the function that needs it. It is generally not a good idea to hide a required parameter that should be passed explicitly. It results in less readable code because it won't make it clear what influences the execution of a certain function.

It is still much better to pass the function down the stack. If the parameters list is getting too long, then it could be grouped into one or more structs in order to be more readable.

Let's have a look at the following function:

func SomeFunc(ctx context.Context, 
name, surname string, age int,
resourceID string, resourceName string) {}

The parameters could be grouped in the following way:

type User struct {
Name string
Surname string
Age int
}

type Resource struct {
ID string
Name string
}

func SomeFunc(ctx context.Context, u User, r Resource) {}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.164.75