4 ms·
that an who the fuck uses interface literals? I do Go personally and professionally for years and never seen it.
by 38 2y ago
that an who the fuck uses interface literals? I do Go personally and professionally for years and never seen it.
- didip 2y agoI was about to say, after so many years doing Go, I have never seen this and would love to read blog post about why interface literal is useful.
- mseepgood 2y agoYou can learn from the Go codebase to get a feel of why this is useful: https://github.com/golang/go/blob/master/src/cmd/go/internal/modfetch/repo.go#L394 https://github.com/golang/go/blob/master/src/cmd/go/internal... https://github.com/golang/go/blob/master/src/io/fs/fs.go#L262 https://github.com/golang/go/blob/master/src/io/fs/fs.go#L26... https://github.com/golang/go/blob/master/src/errors/wrap.go#L18 https://github.com/golang/go/blob/master/src/errors/wrap.go#... https://github.com/golang/go/blob/master/src/go/parser/resolver.go#L49 https://github.com/golang/go/blob/master/src/go/parser/resol...
- metadat 2y agoIf the interface check fails, does the code panic or does the `ok' value just get set to false? If `ok' gets set to false, that seems optimal. Pretty neat, in fact, what other language permits such low-key, easy runtime interface checks?
- 38 2y agothis will panic: v := hello.(world) this never panic: v, ok := hello.(world) and yes, its awesome. people rag on Go because BLARG ERRORS BOILERPLATE, but if you can get over that Go is not Python/JavaScript, you're left with a simple powerful language
- mariusor 2y agoThe Go standard library uses it. I didn't suss out the reason for it though. An example is in the errors package: https://cs.opensource.google/go/go/+/refs/tags/go1.23.0:src/errors/wrap.go;l=58 https://cs.opensource.google/go/go/+/refs/tags/go1.23.0:src/...