There is a default value for everything in Go. Null. 0, "" and nil. As 
someone else said, if you want a parameter to be optional you probably need 
...interface{} and then infer no parameter as 'use the default'. Going a 
little further, you can build default values into a constructor function 
for an interfaced type. 

Oh, probably the neatest solution is to make a struct that lets you input 
the parameters either in-order or with labels instead. Then you can use 
&TypeName{} to mean 'use defaults' or whichever parameters are not 
specified get automatically set to default, either unlabeled and ordered 
such that the values that will be asserted to defaults are not the first 
ones in a struct literal used to feed parameters in. Or make the names nice 
and concise so they aren't troublesome to add (and if your code is going to 
often use defaults, probably you won't even have to specify many values 
very often anyway).

Assertions and labeled parameters are nice features but they don't really 
save you that much time. I would suggest that it's more likely you need to 
rethink the structure of your application and make slightly different named 
parameters for those calls that will use defaults for specific parameters.

Another thing is that you can make null variables imply the use of 
defaults, then you only need to put 'nil' '""' or '0' into these parameters 
and the code will test and fill them automatically. Or if null isn't handy, 
you can define sentinel values for a type that indicate 'use defaults'.

On Wednesday, 22 August 2018 14:39:37 UTC+2, Masoud Ghorbani wrote:
>
> Why there isn't function argument default value in Golang explicitly like 
> Typescript and Python?
>

-- 
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 golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to