Thanks James for your response and the amazing posts. On Mon, May 25, 2020 at 9:01 AM James <[email protected]> wrote:
> This reminds me of functional options which I think are used quite widely > for this purpose. > > See > https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis > and > https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md > > On Mon, 25 May 2020 at 14:57, Amarjeet Anand <[email protected]> > wrote: > >> Is it acceptable to make the optional parameter as varargs? >> I wrote code in my package like ... >> >> >> package backoff >> >> func New(options ...Options) *backOff { >> backOff := defaultBackOff() >> if len(options) == 0 { >> return backOff >> } >> >> // override default values >> if len(options) > 1 { >> panic("only 1 backOffOption allowed") >> } >> optn := options[0] >> backOff.options = &optn >> >> if optn.Delay_ms > 0 { >> backOff.delay_ms = optn.Delay_ms >> } >> return backOff >> } >> >> >> >> So that in other package, it can be used as *backoff.New()* when option >> is not needed(*which will be the case most of the time*). >> >> Is using varargs for optional parameter is ok? Or am I abusing the >> Variadic Functions feature? >> >> >> >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/CANFuhy8nhhbBDWb0%3D7SLvq1aictC9GVG%3D9zpfpJ1gevDdRmd6A%40mail.gmail.com >> <https://groups.google.com/d/msgid/golang-nuts/CANFuhy8nhhbBDWb0%3D7SLvq1aictC9GVG%3D9zpfpJ1gevDdRmd6A%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CANFuhy9Aj%2Bo7M-%3Dstro5aXbjPTnCZ0y_4vYE3CAjDiBpCUhjpw%40mail.gmail.com.
