Hacker News new | ask | show | jobs
by robmccoll 3160 days 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
   }
3 comments

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.

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.)

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++ :)
How is it cleaner and simpler? It reads about the same to me.
It's cleaner because you can't pass invalid options, they won't compile.