How to implement optional function parameters in Go? Analog of overloading from other languages
Hello colleagues! I'm switching to Go from Python and Java, and I've encountered a problem. In those languages, I'm used to function overloading and optional arguments where you can set default values. For example, in Python I would write:
```python
def create_user(name, age=None, city="Moscow"):
...
```
But in Go, I can't simply omit a parameter or write two functions with the same name. I need to create a function for user registration where only `name` is required, and `age` and `city` are optional.
I tried this code, but the compiler complains:
```go
func CreateUser(name string, age int, city string) {
// if age is not passed, I want it to be 0
// if city is not passed, I want "Moscow"
}
func CreateUser(name string) { // error: CreateUser redeclared
CreateUser(na
No answers yet.
Log in to leave an answer
Blogs