faire des paramètres facultatifs
// **Go doesn't support optional parameters natively.**
// Yeah, it is frustrating!
// Link to discussion: https://stackoverflow.com/questions/2032149/optional-parameters-in-go
// * This is a simple hack based on @ferguzz's answer: https://stackoverflow.com/a/19813113/13791656:
package main
import (
"fmt"
)
type CustomOptions struct {
option1 int
option2 string
option3 bool
}
func foo(requiredParam string, optionalParams ...CustomOptions) {
fmt.Println(len(optionalParams)) // returns how many Custom Options are included
option1 = optionalParams[0].option1 // params is an array
fmt.print(optionalParams[0].option1)
}
func main() {
foo("I'm required")
foo("I'm required", CustomOptions(option1: 1))
foo("I'm required", CustomOptions(option1: 1, option2: "2", option3: true))
}
Faithful Flatworm