Go fundamentally does not support optional parameters and default values for function arguments — each function only accepts the set of arguments specified in its signature. This design decision is related to the overall simplicity of the model and predictability of the code.
In other languages, such as Python or C++, optional/default parameters allow for flexible function behavior. In Go, this has been sacrificed for readability and clarity.
When it is necessary to make an API flexible, it is not possible to simply set default values — all parameters must either be explicitly passed or workaround patterns must be used. This complicates maintaining a large number of options and may lead to an increase in the number of overloaded functions.
In Go, two approaches are used for such flexibility:
Example of the structure approach:
type QueryOptions struct { Limit int Offset int } func QueryDB(opts QueryOptions) { if opts.Limit == 0 { opts.Limit = 10 // default } // ... } QueryDB(QueryOptions{Limit: 100})
Or through functional options:
type Config struct { Timeout int } type Option func(*Config) func WithTimeout(t int) Option { return func(cfg *Config) { cfg.Timeout = t } } func Do(opts ...Option) { cfg := Config{Timeout: 5} // default for _, o := range opts { o(&cfg) } // ... } Do(WithTimeout(10)) // call with option Do() // call with default
Key features:
Can you set a default value when declaring a function, for example func F(a int = 10)?
No, such a declaration is not possible in Go — only a strict list of required parameters is allowed.
What happens if a function is declared with a slice of type ...interface{} and it is passed 0 arguments?
The slice will have a length of 0 (nil), and the function will receive an empty slice.
Example code:
func PrintAll(args ...interface{}) { fmt.Println(len(args)) // 0 if no parameters are passed } PrintAll() // ok
Is it possible to overload functions by the number or type of parameters in Go?
No, function overloading in Go is not supported — duplicate function names with different signatures are not allowed.
An API function has dozens of parameters, many of them of the same type, causing mistakes due to confusing order:
Pros:
Cons:
A parameter structure or functional options is used, parameters have explicit names:
Pros:
Cons: