How to implement optional function parameters in Go? (analog of overloading)
Hello everyone! I've just started learning Go after Java and Python, and I've encountered a problem that's really stumping me.\n\nIn the languages I knew before, it was easy to declare functions with optional parameters. For example, in Python I could write: def connect(host, port=8080, timeout=30). And call it as connect("localhost"), connect("localhost", 9090) or connect("localhost", 9090, 60).\n\nNow I'm trying to do the same in Go. I need to write a function for connecting to a server, where port and timeout should be optional. I tried the obvious approach:\n\n```go\nfunc connect(host string, port int = 8080, timeout int = 30) {\n // ... connection code\n}\n```\n\nBut the compiler gives an error: "syntax error: unexpected =, expecting {". It turns out Go doesn't have default
No answers yet.
Log in to leave an answer