The Go language itself does not support setting default values for function parameters (unlike Python or other languages). However, you can simulate the default value function of function parameters in the following ways:
1. Pass optional values through pointers
If the function receives a pointer as an argument, you can passnil
to simulate unpassed cases and provide default values inside the function.
package main import "fmt" func greet(name *string) { if name == nil { defaultName := "Guest" name = &defaultName } ("Hello,", *name) } func main() { greet(nil) // Use default values greet(&"Alice") // Use incoming values}
Output:
Hello, Guest
Hello, Alice
2. Use structure to simulate default parameter values
You can define a structure as the parameter type of the function, and then set the default value for the structure field according to the situation.
package main import "fmt" type Config struct { Name string Age int City string } // default valuefunc NewConfig() *Config { return &Config{ Name: "John Doe", // default value Age: 30, // default value City: "New York", // default value } } func Greet(cfg *Config) { if cfg == nil { cfg = NewConfig() // If there is no incoming configuration, use the default value } ("Name: %s, Age: %d, City: %s\n", , , ) } func main() { // Use default configuration Greet(nil) // Custom configuration customCfg := &Config{Name: "Alice", Age: 25} Greet(customCfg) }
Output:
Name: John Doe, Age: 30, City: New York
Name: Alice, Age: 25, City: New York
3. Use variable length parameters (...) and custom logic (recommended)
You can also use Go's variable-length parameters to simulate the effects of default values. When calling a function, if no parameter is provided, you can check the number of parameters within the function and assign the default value.
package main import "fmt" func greet(args ...string) { if len(args) == 0 { ("Hello, Guest!") } else { ("Hello,", args[0]) } } func main() { greet() // Use default values greet("Alice") // Use incoming values}
Output:
Hello, Guest!
Hello, Alice
4. Use option mode (constructor)
Another common way is to use the constructor pattern, which is often useful when multiple optional parameters are needed. You can set all possible parameters by creating a function and then pass only some of the parameters you want.
package main import "fmt" type Person struct { Name string Age int Email string } func NewPerson(name string, options ...func(*Person)) *Person { p := &Person{Name: name} // Required parameters // Set optional parameters through options for _, option := range options { option(p) } return p } // Optional settings for Age and Emailfunc WithAge(age int) func(*Person) { return func(p *Person) { = age } } func WithEmail(email string) func(*Person) { return func(p *Person) { = email } } func main() { p1 := NewPerson("John") // Only Name (p1) // Output: &{John 0 } p2 := NewPerson("Alice", WithAge(30), WithEmail("alice@")) (p2) // Output: &{Alice 30 alice@}}
Output:
&{John 0 }
&{Alice 30 alice@}
Summarize:
Although Go does not directly support default parameter values, you can use the following methods to achieve similar effects:
Use structures and pointers to simulate default values. Use variable length parameters and set the default value according to the number of parameters. Use the option mode (constructor), set the default value by passing optional parameters.
This is the article about how to set the default value of go function parameters. For more relevant go function parameters, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!