3 ms·
Sorry, if your position is "Golang doesn't use getters to abstract over structure fields", that's just not true. Here are some examples: - https://golang.org/p
by pcwalton 12y ago
Sorry, if your position is "Golang doesn't use getters to abstract over structure fields", that's just not true. Here are some examples:
- https://golang.org/pkg/database/sql/driver/#Rows https://golang.org/pkg/database/sql/driver/#Rows -- Columns is a getter
- https://golang.org/pkg/database/sql/driver/#Stmt https://golang.org/pkg/database/sql/driver/#Stmt -- NumInput is a getter
- https://golang.org/pkg/image/#Image https://golang.org/pkg/image/#Image -- ColorModel and Bounds are getters
- https://golang.org/pkg/reflect/#Type https://golang.org/pkg/reflect/#Type -- Align, FieldAlign, NumMethod, Name, PkgPath, Size, Kind, Bits, ChanDir, IsVariadic, Elem, Key, Len, NumField, NumIn, NumOut are all getters
And so on.
The reflect package could, for example, have the Type-returning methods return a struct using structure embedding to handle most of those fields. But they didn't write the API that way, because it would be annoying to do so. So they used interfaces with getters instead. This isn't Java "astronaut" design—it's just how you program in a language with interfaces.
Look at the implementation of PkgPath() for example: https://golang.org/src/reflect/type.go#L446 https://golang.org/src/reflect/type.go#L446
There's only one implementation of PkgPath(), and it does nothing but address a struct field. The functions could expose uncommonType directly, but they didn't. That's because getters are a common design pattern in Go.
- tptacek 12y agoIt is absolutely not idiomatic Golang code to use getters and setters rather than struct fields, as I am painfully aware after a day wasted wrestling with net/http and httprouter. Golang strongly prefers simple struct fields. You correctly observe than in order to conform to a Golang interface, struct fields have to be exposed through setters and getters. But all those examples are interfaces, not structs. The gap between your summary here and the Golang idiom is that Golang doesn't want you to make everything an interface. Generally, Golang interfaces seem to fit one of two molds: * Interfaces that any systems programmer could predict needing ahead of time, like io.Reader and io.Writer. * Interfaces that are born "bottom up" to allow for a very specific bit of code reuse. Golang's standard library does not have a lot of interfaces that are conceived of as hooks for future functionality and opportunities for decoupling, which is a stark contrast from Java libraries, which do.
- pcwalton 12y agoThose examples I gave all could have been structs, but weren't. Image.Bounds() and Type.IsVariadic() could have been plain struct fields, for instance. Grep through the Go source code for "func Bounds()" and observe that every single implementation of that interface is "return p.Rect" (well, except Uniform, but that one could have been a field too). Sure, it'd require some contortions to factor out your code so that interfaces work, but that's precisely my point: getters are a natural way to program with interfaces in every language. Does Java tend to use getters unnecessarily where fields could have sufficed? Absolutely. Go does not, and that's a great thing about the language. But I think that's irrelevant to the point at hand about explicit interface names. In none of the cases where the name collision problem was an issue would moving things to fields have helped. For Next() in Golang, it was actually a method with behavior that was causing the problem, not a simple getter. For getName() in Java, the problem was that two different libraries that needed to work abstractly with a user object needed different "views" of the same field to work properly, and there was no way to disambiguate—observe that with fields, you'd have the same problem! Likewise for the Android library. In these cases, you really want explicit interfaces. The right objection, in my mind, is not "just use fields" but is instead "use the adapter pattern". That is, acknowledge that yes, this is a problem in practice, but when it arises you can just make a separate type wrapping your type and implement different methods on that wrapper. That is indeed a solution that works around the issue. The question from a language design POV is then whether the benefits of structural interfaces outweigh the downsides, one of which is requiring programmers to write adapters to solve name conflicts. Reasonable people can differ on that, and I think it's obvious I disagree with Rob Pike here. But presenting fields as an adequate substitute for getters is inadequate (since you really do need getters), unappealing (since getters are great, which is why Go uses them) and pretty much totally tangential to the problem.
- NateDad 12y agoThe thing is, implicit interfaces give you so much freedom and make it so easy to keep your code decoupled, that the very rare times that you hit something like this Next() problem, you just work around it and accept it as a cost for all the other benefits it brings. For the record, in Juju we have hundreds of interfaces and hundreds of thousands of lines of code, and AFAIK have never had this be a problem. For the given Next problem, you can easily make a wrapper type that simply masks the underlying Next() method with its own implementation. type MyType struct {} func (m *MyType) Next(i int) { // do base next stuff } type ForSql struct { *MyType } func (f ForSql) Next() bool { // do sql-next stuff } and then using this type is trivial in-code: mt := &MyType{} doSomeSqlThing(ForSql{mt}) Note that this new type retains all the methods of the original type, so it still implements all the interfaces it used to (unless they rely on the old Next signature). Compare this to the benefits of implicit interfaces (decoupled code, easy composition, using third party code as your own interface type without needing a wrapper type)... hopefully it's pretty obvious this is a large net positive.