The Problem with Many Params
Why constructors get messy.
Why Constructors Get Messy
Go has no default arguments or named parameters. As a type gains options, its constructor signature grows unwieldy and error-prone.
A Constructor Explosion
Imagine a server with host, port, timeout, TLS, and retries. A single constructor needs all of them.
package main
import "fmt"
func NewServer(host string, port, timeout, retries int, tls bool) {
fmt.Println(host, port, timeout, retries, tls)
}
func main() {
NewServer("localhost", 8080, 30, 3, false)
}All lessons in this course
- The Problem with Many Params
- Option Functions
- Building a Flexible API
- Defaults and Validation