Re:Reusable and type-safe options for Go APIs
by robmccoll on Sunday October 22, 2017 @12:21PM
(#15527499)
How is it not cleaner and simpler to just use something of a builder pattern?
[ Parent
| on HN ]
Re:Reusable and type-safe options for Go APIs
by everdev on Sunday October 22, 2017 @01:05PM
(#15527750)
In 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.
Re:Reusable and type-safe options for Go APIs
by eternalban on Sunday October 22, 2017 @01:58PM
(#15528035)
Clearly 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.)
Re:Reusable and type-safe options for Go APIs
by jopsen on Monday October 23, 2017 @02:59AM
(#15531246)
Or
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++ :)
Re:Reusable and type-safe options for Go APIs
by eikenberry on Sunday October 22, 2017 @03:29PM
(#15528467)
How is it cleaner and simpler? It reads about the same to me.
Re:Reusable and type-safe options for Go APIs
by cormacrelf on Monday October 23, 2017 @05:50AM
(#15531937)
It's cleaner because you can't pass invalid options, they won't compile.