5 ms·
Because Go doesn't support userland generic types, I see liberal use of `interface{}` types in APIs. There must be a better way.
by acchow 5y ago
Because Go doesn't support userland generic types, I see liberal use of `interface{}` types in APIs. There must be a better way.
- eko 5y agoGenerics are coming in next version, go 1.18 in around 6 months ;)
- elementi 5y agoThat would be such great! I'm pretty new with Golang, where could I keep myself updated with the latest news?
- henvic 5y agoI'd say keeping an eye in the issue tracker https://golang.org/issues https://golang.org/issues + golang-nuts mailing list https://groups.google.com/g/golang-nuts?pli=1 https://groups.google.com/g/golang-nuts?pli=1 might be a good start. I wrote an article about where to find Go-related content a while ago: https://henvic.dev/posts/go/ https://henvic.dev/posts/go/
- elementi 5y agoThanks a lot!!!
- Abishek_Muthian 5y agoI recommend https://golangnews.com/ https://golangnews.com/ too, Not just because of its utility or that it has HN interface but also because Kenny Grant has been contributing to the Go ecosystem for a long time and his code is a pleasure to read.
- acchow 5y agoanxiously awaiting :)
- qaq 5y agoWell they will be landing pretty soon
- sabhiram 5y agoThere is nothing wrong with a well documented interface. The `is-a` pattern and `has-a` pattern are all I have ever needed when composing "objects" in golang.
- acchow 5y agoI guess I see it mostly used without accompanying documentation
- sabhiram 5y agoUsually, the `interface{}` is named something like `Xer` to denote an interface which satisfies `X`. So - `Dialer` would be the interface and any struct is free to implement the `Dialer` methods to qualify as one (for example). Presumably requiring some way to `Dial` something. What I like most about `go` is how standard a lot of the "good practices" are, with a focus on getting stuff implemented. There is no time to argue about spacing and bracing, just run `go fmt`.
- umvi 5y agoThe "better way" is coming in an upcoming go release (generics)
- joshuak 5y agoI find generics are overused in most languages. Go will be getting generics to handle the very few situations in which they are a genuine value. But the most common uses cases for generics in other langues are already handled by Go interfaces. For example Go's `sort.Interface` provides a mechanism to implement generic sort, find and set operations on any type of collection that implements those three methods (Len, Swap, and Less). I nearly never find myself having to use `interface{}` in my Go code, and those that do usually don't have to, they just don't want to define an appropriate interface.
- sethammons 5y agoinstead of taking an empty interface, take an interface with defined methods. Then the caller can conform their type to whatever the API needs.
- acchow 5y agoThis is what people due when generic types (parametric polymorphism) wouldn't be involved. But if you need type parameters, then you wouldn't be able to do this.
- bediger4000 5y agoI dread the appearance of generics. It's not just that they lead to longer compile times for very little benefit, it's that everybody wants every thing to be generic, just in case. Like the C++ dogmas around how many of which type of constructor you need, generics encourage fast-spreading, deleterious memes.