Is there a way to declare an argument as โoptionalโ in the Go programming language?
An example of what I mean:
func doSomething(foo string, bar int) bool { //... }
I want the bar parameter to be optional and defaults to 0 if nothing went wrong.
bar
0
doSomething("foo")
will be the same as
doSomething("foo",0)
I cannot find anything about this in the official function documentation.
I do not believe that Go supports optional arguments for functions, although you can fake it with variadic functions . C approach, if you don't want to do this, is to pretend that the language supports currying:
func doSomethingNormally(foo string) bool { doSomething(foo, 0) }
Another way to fake this is to convey structure.
type dsArgs struct { foo string bar int } func doSomething(fb dsArgs) bool { //... }
Then
doSomething(dsArgs{foo: "foo"})
coincides with
doSomething(dsArgs{foo: "foo", bar: 0})
Source: https://habr.com/ru/post/1388649/More articles:Iphone USSD connection in background - iosIncreasing a variable's value using onClick in javascript and displaying a new value in an HTML form - javascriptPython: scope of variables and profile.run - variablesHighlight selected JList index - javaHow can I create a Python Sybase module on Windows? - pythonbash string to an array with spaces and extra delimiters - stringhow to connect to Outlook using .net framework - c #Computing combinations of length k from a list of length n using recursion - pythonData error while reading csv file in c # winforms - c #SQL, comparing relationships hosted in multiple link tables - sqlAll Articles