5 ms·
Note that this is not necessarily a great thing to overuse. Also note that the article is 10 years old. Relying on such upgrades sort of introduces a dark and
by rollulus 2y ago
Note that this is not necessarily a great thing to overuse. Also note that the article is 10 years old.
Relying on such upgrades sort of introduces a dark and fuzzy part of the API. Go’s http pkg is a notorious one, with how a http.ResponseWriter can also be a Flusher and Hijacker and I don’t know what else. If you in your middleware want to wrap it, you need to implement those interfaces as well, or the functionality is lost. But they’re not part of the “visible” API, they’re type assertions buried deep in the standard library, good luck with that. For this reason Go 1.20 introduced the http.ResponseController.
- _yy01 2y agoI encountered this and went through some corners of the standard library. shameless plug to my own blog post: https://mahesh-hegde.github.io/posts/go-interface-smuggling/ https://mahesh-hegde.github.io/posts/go-interface-smuggling/
- rollulus 2y agoIf you never encountered this you never created your own ResponseWriter, or you did it and inadvertently broke its Flushing, Pushing and Hijacking capabilities. It’s not more complicated than this.
- quectophoton 2y agoWait what. For functionality like that[1] I would have expected them to fallback to `bufio.Peek` if the `fs.File` is not an `io.Seeker`. Sure, it's a bit slower, but it would just work. If someone implements custom `fs.File`, then it's up to them implementing any additional interface that allows better performance. It's the same idea as `io.Copy` trying to use first `io.WriterTo` or `io.ReaderFrom` if implemented, and then falling back to a manual copy. The thought process should be: First try to ask for an interface that has all methods you require (that's like the whole point of an interface). If you can't do that, then try to work with the methods you have, and then mention that performance can be improved if additional interfaces are implemented. Only if when you can't do that, you fail with a runtime error complaining about missing methods you didn't ask for in the interface signature. I might be missing some reason that prevents them from using `bufio.Peek` though. [1]: Link to the use of Seek, for reference: https://github.com/golang/go/lob/e8ee1dc4f9e2632ba1018610d1a1187743ae397f/src/net/http/fs.go#L289 https://github.com/golang/go/lob/e8ee1dc4f9e2632ba1018610d1a...