5 ms·
How is it not cleaner and simpler to just use something of a builder pattern? srv = NewServer(addr).WithTLS(crt,key).WithRateLimit(30).WithPool(10).Start()
by robmccoll 9y ago
How is it not cleaner and simpler to just use something of a builder pattern?
srv = NewServer(addr).WithTLS(crt,key).WithRateLimit(30).WithPool(10).Start()
if srv.Err() {
// Handle
}
- everdev 9y agoIn Go, chaining is not idiomatic. For one, it becomes more challenging to determine where the error occurred if you want to try to handle the error instead of failing. Also, it requires each function in the chain to detect if an error has already occurred. Interfaces are generally a better solution.
- eternalban 9y agoClearly a fluid API can not return a 2+ tuple as its results so the error issue you note is a non-issue. The clean way to deal with errors in fluid APIs is to have a terminal .init() that returns an error (which can also specifically note which options are "illegal", etc.)
- deleted 9y ago[deleted]
- eikenberry 9y agoHow is it cleaner and simpler? It reads about the same to me.
- cormacrelf 9y agoIt's cleaner because you can't pass invalid options, they won't compile.
- jopsen 9y agoOr NewServer(Options{Addr: addr, RateLimit 30, Pool: 10}) IMO, keep it simple and remove the optional features if possible is also a good choice. If my golang API is complex, then I might aswell have done it in C++ :)