TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Reusable and type-safe options for Go APIs

79 pointsby derekchiangover 7 years ago

5 comments

sethammonsover 7 years ago
I find I don&#x27;t care for implementing the Options paradigm. More often then not, the things I&#x27;ve implemented have required parameters, and sometimes many of them. Mixing required and options often feels kludgy. I feel the Options method requires lots of documentation referencing, as opposed to more discoverable (via your editor) constructors. I tend to use explicit parameters or a struct with validators where every field is expected to be explicitly set.<p>This is not to say that using Options is wrong. Some things are highly customizable and having too many constructors would not be an appropriate solution. Worse would be a struct based config where you only set some fields some times. I just don&#x27;t find myself creating these kinds of constructs. My most configurable things are usually server instances.<p>For those who do use the Options paradigm and find it useful, what are you creating?
评论 #15527025 未加载
评论 #15527948 未加载
评论 #15531212 未加载
sfifsover 7 years ago
I&#x27;ve been using Go as my preferred language for almost a decade now. Hackish &quot;clever&quot; solutions like these coming from designers of the language like Rob Pike and Dace Cheney simply indicate that the language is hitting its limits of expressivity and needs to start thinking hard about overloading which is the right solution to problems like this.
评论 #15525862 未加载
评论 #15526498 未加载
评论 #15525852 未加载
评论 #15526052 未加载
jopsenover 7 years ago
Once you accept that the golang type system can&#x27;t enforce everything your life will be easier.<p>Use rust or Haskell if you want a powerful type system that can do fancy things.<p>golang is great, but you have to accept that some things are runtime errors, and that&#x27;s fine, it keeps things simple. How often are options given conditionally anyways? Simple tests will cover this in most cases.
评论 #15525938 未加载
评论 #15526201 未加载
robmccollover 7 years ago
How is it not cleaner and simpler to just use something of a builder pattern?<p><pre><code> srv = NewServer(addr).WithTLS(crt,key).WithRateLimit(30).WithPool(10).Start() if srv.Err() { &#x2F;&#x2F; Handle }</code></pre>
评论 #15527750 未加载
评论 #15531246 未加载
评论 #15528467 未加载
tdrdover 7 years ago
The downside to this solution is that the concrete option types are exported. I believe it&#x27;s possible to return unexported types from exported functions, which would solve this, but that golint complains about that pattern.<p>Have you suggested this to the etcd maintainers?
评论 #15534023 未加载