Unlike C# you can put "Optional" keyword to add optional arguments in function, there is no direct way to create optional parameters in Go functions. And there is no method overloading in Go either. Even so, if you really need to add optional arguments to an existing function, maybe because this function is in a common library which is called by different software modules and you don't want to update all the modules for this new argument change, there is still a way to implement optional arguments thanks to variadic arguments in Go functions.
Variadic arguments allow developer to put any number of trailing arguments as the input of functions. For example:
package main
import (
"fmt"
)
func main() {
fmt.Printf("1 + 2 = %d\n", add(1, 2))
fmt.Printf("1 + 2 + 3 + 4 + 5 = %d\n", add(1, 2, 3, 4, 5))
}
func add(args ...int) int {
sum := 0
for i:=0; i < len(args); i++ {
sum += args[i]
}
return sum
}
The output is:
1 + 2 = 3
1 + 2 + 3 + 4 + 5 = 15
With the help of variadic arguments, you can easily to implement optional arguments. For example, you have a function to generate characters in a common library for a game development. This function can generate characters with name and sex for now.
package main
import (
"fmt"
)
func main() {
createCharactor("Bertram", "Male")
}
func createCharactor(name string, sex string) {
fmt.Println("---------- New Charactor ----------")
fmt.Printf("Name is: %s\n", name)
fmt.Printf("Sex is: %s\n", sex)
fmt.Println("------ New Charactor Created ------")
}
The output is:
---------- New Charactor ----------
Name is: Bertram
Sex is: Male
------ New Charactor Created ------
Now you want to add age argument to this function without impacting the existing function call. You can do like following:
package main
import (
"fmt"
)
func main() {
createCharactor("Bertram", "Male")
fmt.Println()
createCharactor("Kai", "Male", 15)
}
func createCharactor(name string, sex string, age ...int) {
fmt.Println("---------- New Charactor ----------")
fmt.Printf("Name is: %s\n", name)
fmt.Printf("Sex is: %s\n", sex)
if len(age) > 0 {
fmt.Printf("Age is: %d\n", age[0])
}
fmt.Println("------ New Charactor Created ------")
}
The output is:
---------- New Charactor ----------
Name is: Bertram
Sex is: Male
------ New Charactor Created ------
---------- New Charactor ----------
Name is: Kai
Sex is: Male
Age is: 15
------ New Charactor Created ------
But the above code is not reusable to add other arguments with different data types. For example, what if you need to add title, email later? So in order to make the variadic argument more generic, we can use "interface{}" type as the variadic argument which gives the ability to add more arguments with different data types.
According to this, the above code can be refactored as below:
package main
import (
"fmt"
)
func main() {
createCharactor("Bertram", "Male")
fmt.Println()
createCharactor("Kai", "Male", 15, "Software Engineer", "kai@gmail.com")
fmt.Println()
}
func createCharactor(name string, sex string, args ...interface{}) {
fmt.Println("---------- New Charactor ----------")
fmt.Printf("Name is: %s\n", name)
fmt.Printf("Sex is: %s\n", sex)
if len(args) > 0 {
fmt.Printf("Age is: %d\n", args[0].(int))
fmt.Printf("Tile is: %s\n", args[1].(string))
fmt.Printf("Email is: %s\n", args[2].(string))
}
fmt.Println("------ New Charactor Created ------")
}
The output is:
---------- New Charactor ----------
Name is: Bertram
Sex is: Male
------ New Charactor Created ------
---------- New Charactor ----------
Name is: Kai
Sex is: Male
Age is: 15
Tile is: Software Engineer
Email is: kai@gmail.com
------ New Charactor Created ------
However, this solution does not convey the meaning of the optional arguments. So you either put a comment above the header to specify what it means for each optional argument, or comment where you call the functions, otherwise it will become very difficult to understand the code. So this solution is not recommended unless you have a very tight deadline to meet the delivery date and really have no time to refactor code.