lundi 20 janvier 2020

Golang - passing struct instead of keeping a long list of parameters for a function

I have come across a scenario where I have to write a function in Golang with more than 11 parameters. From the readability point of view, I don't think it's a good idea to keep all the 11 parameters in the function declaration.

I was thinking of declaring a struct with all the parameters and pass that struct to the function. Is it a good approach? or Is there any other ways to do this ?

Ex:

type Params struct {
 Name string
 Type string
}

func (p * Params) Validate() {
  // validation logic
}

func GetRequest(p *Params) {
  p.Validate()
 // function logic
}

Aucun commentaire:

Enregistrer un commentaire