4 ms·
In inheritance OOP languages, interfaces are an implementation detail of the class. In Golang, they are proxies. You have to think about them differently. Also
by dbt00 6y ago
In inheritance OOP languages, interfaces are an implementation detail of the class. In Golang, they are proxies. You have to think about them differently.
Also I agree with the other commenter that all this verbosity isn't very idiomatic. If you can't just write if a != expected { t.Failf(...) } for each thing, then use testify, because they definitely have been thinking about this long enough to get it right:
github.com/stretchr/testify
- jrockway 6y agotestify gets basically everything about Go testing wrong. It even takes its arguments in the wrong order. https://github.com/golang/go/wiki/TestComments#assert-libraries https://github.com/golang/go/wiki/TestComments#assert-librar... https://github.com/golang/go/wiki/TestComments#got-before-want https://github.com/golang/go/wiki/TestComments#got-before-wa... They did at least: https://github.com/golang/go/wiki/TestComments#compare-full-structures https://github.com/golang/go/wiki/TestComments#compare-full-... Although the diff-ing is significantly more limited than cmp.
- nvarsj 6y agoWow when did they add that to TestComments? I really disagree though. The native testing framework is overly clunky and verbose. It also makes things harder to debug when they fail. Just look at the example it gives! You don't even know why the test failed, it combines 4 different tests in one line. Are you supposed to inspect the entire object that gets printed out to figure it out? How terrible.
- jrockway 6y agoYeah, it's a really bad example. I would just cmp.Diff, and if I only cared about 4 fields, I would delete the data from got.FieldIDontCareAbout before comparison. (You can also make cmp.Diff do this if you want to do it every time.) I think that an evolution of tests would be: assert.Equals(got.Bar, want.Bar) assert.Equals(got.Baz, want.Baz) then: if got.Bar != want.Bar || got.Baz != want.Baz { t.Fatalf("bar and baz:\n got: %v\n want: %v", got, want) } then: want := Thing{Bar: xxx, Baz: yyy} got.Quux = "" if diff := cmp.Diff(got, want); diff != "" { t.Fatal("bar and baz:\n%s", diff) } (cmp.Ignore and friends should also be considered for less misleading output, if you don't care about Quux) Basically, cmp.Diff should be your go-to testing framework. It's easy to use and the output is excellent.