9 ms·
Down the Golang Nil Rabbit Hole
- loopz 6y agoTrying to use Go as a declarative language seems to me ill-suited. The language works best when fully embracing imperative forms. You can use interface, composition and function values for effectful stuff, but it's no DSL.
- shp0ngle 6y agoYeah, I think the `detest` package is using wrong tool for the job. (And there is already testify and diff.)
- Cthulhu_ 6y agoI looked at the code and it did not feel good. Go's ecosystem focuses on line-of-sight coding, that is, happy path has the least indentation, unhappy path (e.g. test failures in the naive / default unit test approach) should be indented. This tool seems to do a lot of indentation. I know it's a bit of an arbitrary thing, but it's a good rule of thumb. And it's consistent with the other 99% of Go code, which is really important.
- nemothekid 6y agoYou can hit this bug without using reflect. It, to me, is Go's third biggest wart. I'm particularly fond of this bug, because I got bit by it and it crashed prod during an important sales demo. package main type Compressor interface { compress() []byte } type GZipComprosser struct { internal byte } func (c *GZipComprosser) compress() []byte { return []byte{c.internal} } func DoCompress(c Compressor) []byte { if c == nil { return []byte{} } return c.compress() } func main() { var c *GZipComprosser c = nil DoCompress(c) } This is the "nil interface" trap and will crash with an NPE even though are "checking" nil.
- saghm 6y ago> It, to me, is Go's third biggest wart. Out of curiosity, what are the first two?
- j-james 6y agoLack of generics and clumsy error handling would be my guess.
- BobbyJo 6y agoIn practice, those have honestly been the two largest assets to me. No generics means not worrying about a coworker going off the rails and creating a library where it's not needed (me being that coworker often in my past), and clumsy error handling means factoring said error handling into your APIs, making you much more deliberate about where and how errors are eaten. Having used Java extensively, those two 'warts' have honestly saved me a lot of heartache.
- somezero 6y agoHow much do your colleagues (ab)use goto statements that Go provides? :)
- brundolf 6y agoYikes. This is like JavaScript's null/undefined, but even worse because they don't even have distinct names. No thank you
- dbt00 6y agonil is just the empty value for slices maps pointers interfaces functions and channels. the only slightly confusing part is that interface objects are proxies that wrap a pointer and a type, so a nil pointer inside an interface is non-nil. Writing idiomatic go this does come up but not nearly as when you do stuff like this.
- Kiro 6y ago> is just Considering you replied to a JavaScript comparison I, as a JS dev, would expect to understand at least one word of what you just wrote. If this is the easy explanation I think Go is out of my league.
- vineyardmike 6y agoThat’s the wrong attitude! Go is not out of anyone’s league. Certainly not someone who already can program. Not understanding a comment on HN is not particularly surprising and need not be a proxy for ability. I think this is unfairly self critical. (At least for anyone else reading this, maybe the original poster meant it in jest).
- why_Mr_Anderson 6y agoEvery time I see any Go code, it looks like the language was designed with primary goal being as different from everything else as possible. If there is X ways something is done (and you could then relate to that knowledge), you can be sure Go uses something different.
- hnlmorg 6y agoGenuine question: what languages have you tried and are comparing Go against? I ask because if anything, Go is as mediocre as they come with regards to it's design. There's very little about Go that stands out as being different. For many, that's it's attraction: it doesn't try to do much different and instead focuses on the basics. For others that's what's off putting about Go: it's basic and doesn't offer anything new or different. Also comparing Go to languages like Javascript is never going to end well because they're entirely different paradigms in almost every category aside the C-like syntax (dynamic vs AOT statically compiled, functional and object orientated vs imperative, loosely typed vs strictly typed, multi-threaded (albeit with green threads) vs single threaded).
- dbt00 6y agoIn 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.
- brianberns 6y agoNull references: the billion dollar mistake.
- masklinn 6y agoTyped nils: when you think a billion dollar mistake is not enough money.
- sn41 6y agoI think that a proper understanding, and awareness of the import of, of nil, null, None and other empty/bottom types and values is a very pressing need in programming languages and databases (see the intricate discussion on SQL NULL [1]). It is wrong to dismiss this as mere "WTF" caused by pragmatic implementation goof-ups. There is a deep philosophical issue here which needs to be one of the first issues to tackle when implementing a system, rather than an after thought. An analogy I can think of is recursion: the base case needs as careful a thought as the inductive case, otherwise the code will go into an infinite loop. Empty values and types are probably the base case of a type system, and have to be handled meticulously and in a logically sound manner from a programming language theory persepective. [1] https://en.wikipedia.org/wiki/Null_(SQL) https://en.wikipedia.org/wiki/Null_(SQL)
- wwwigham 6y agoToo bad nil/null _aren't_ typically bottom types or "base cases", but rather are real, runtime values that are sentinel values of some kind, rather than valid references to a value of some type. A distinct sentinel value that, for some usually historical reason, your compiler often thinks is OK to pretend is some interface, right up until it blows up at runtime. I get the confusion, though, since the same compilers that make that mistake often attribute some bottom-type-like characteristics to nil/null to facilitate using it (eg, considering it a valid possible value of all other expressable interfaces). Empty values and other sentinels are meaningfully different from a type-theoretic bottom type. Real bottom types don't come up much in mainstream languages - TypeScript has `never`, Scala has `Nothing`, and both typesystems have distinct representations of various `null`s and other empty values. Such uninhabitable bottom types see most of their use in describing generic constraints in situations where variance comes into play. I think the real "WTF" here is that the go compiler quietly makes a simple `nil` written in your code into one of potentially many nil-like sentinels at runtime (contrary to what one may think), coerced depending on usage, and, moreover, that a simple `==` comparison or reflective call alone is insufficient to detect all of them. The semantics of how such a sentinel is coerced (and reflected) is surely in the realm of language esoterica and not simple beginner knowledge.
- lifeisstillgood 6y ago
- kstenerud 6y agoI'm hitting the same kind of problem writing serde code in go, trying to marshal/unmarshal data to/from structs. The biggest problem is how SLOW reflect is, requiring all sorts of complicated caching to ensure the least number of reflect calls are made in aggregate (have a look at the JSON codec in the standard library for an idea). The other terrible part is io.Writer. Since these are interfaces, the compiler can't tell what the actual implementation will do with the []byte parameter, even though we humans know perfectly well. So if you try to write from a stack allocated buffer, the compiler will hoist it into the heap, every time (and allocations are one of the slowest parts of go). I started off by writing a complicated heap-based buffering mechanism until finally I said to myself "nah... it can't be THAT bad - they've got to have some kind of mitigation for this!" So I changed everything to very simple writer.Write, and the serialization performance dropped by more than half (from 1700 ns/op to 3500 ns/op). Allocations went from 5 per op to 37 per op! Would be nice to have a "nostore" modifier for slices to tell the compiler that the receiver is not allowed to store it (thus preventing this constant stack-to-heap copying).
- marcus_holmes 6y agoI've arrived at the opinion that any use of reflect is a red flag that the author is trying to do something that Go wasn't designed to do and the code will probably be extremely flaky. It's not a 100% match, but it's a good heuristic in most cases. As my experience with Go has increased, the number of times I need to touch reflect has decreased.
- christophilus 6y agoHow do you marshal/unmarshal JSON?
- nvarsj 6y agoYou write a custom marshal/unmarshal method for every type, of course.
- 6y ago
- Someone 6y agoSince this thread started discussing warts and has people versed in go comment, can anybody explain what we have https://golang.org/src/strings/compare.go https://golang.org/src/strings/compare.go? Why is “symmetry with package bytes” so important that it is deemed a good idea to add a function that “no one should use” _and_ intentionally make that function slow? Also, if that’s a good idea, why doesn’t https://golang.org/pkg/strings/#Compare https://golang.org/pkg/strings/#Compare shout out harder that that function shouldn’t be used? (Came across that when googling whether string comparison in go uses locale)
- hardwaresofton 6y agoThere are very good reasons to pick Golang and "correctness" is not one of them. Go is very pragmatic and it seems like most people are very prepared to forgive a lot of corners like this for a language that is succinct, hard to be clever in, easy/fast to compile, and performant. While we're here I'd love to talk about Go's lack of memory pressure control/feedback though. How people are working around it/using Go in production -- is everyone just right/over-provisioning servers? using `ulimit memlock`? Hoping on swap? I ask because I just ran into this working on a tiny project whose only job was to serve a single file[0] (with few resources) and was immediately bit by the OOM killer (I used docker to artificially limit resources). [EDIT] I can't help but add this, as the language hipster/elitist I am. Don't pick languages that have nil/null in them in 2021 (and after) if you can help it. Strictness (or "correctness") around values starts with nil/null/undefined. That said, if you actually want to produce valuable software and produce value, use whatever the fuck gets you to v1 without too much suffering. [0]: https://gitlab.com/mrman/kcup-go/ https://gitlab.com/mrman/kcup-go/
- arethuza 6y agoDo you mean the language specification is succinct or that code in the language is succinct - I thought Go favoured the former over the latter? Edit: No implied criticism of Go intended - while not to my personal taste it seems like a fine language that a lot of people clearly get a lot of value from.
- hardwaresofton 6y ago> Do you mean the language specification is succinct or that code in the language is succinct - I thought Go favoured the former over the latter? Succinct was the wrong word -- for complex things Go is definitely not succinct in that sense you're absolutely right. It's more succinct than Java for doing what I wanted to do (waiting for someone to drop a nice tight Jersey/JAX-RS example in 3...2....1...), but what I should have written was "simple" -- Go code is simple to read and it's much harder to strangle the rest of your team (and your future self) with complexity. > Edit: No implied criticism of Go intended - while not to my personal taste it seems like a fine language that a lot of people clearly get a lot of value from. I would disagree on it being a fine language, but I have watched enough of rob pike and go and the team members' public talks to know that this is exactly what they intended and that's great. It's a pragmatic, powerful language with good performance, and people are deriving a TON of value from it. To be fair I should also note that I am the exact kind of person who Go is not built for -- a yak shaver and amateur type theorist. I'd pick Go over Java in a heartbeat, but I think there are some other good choices out there if you're free to roam.
- ucirello 6y agoOff-topic re: OP blog's and domain name. These are nice and subtle nods to the tetralogy of the Book of New Sun. House Absolute is the palace of the Autarch, the ruler of the Commonwealth, a southern hemisphere nation in a far future when the Sun is dimmed. (If my memory serves me well...)
- one2three4 6y agoThings like that make me shake my head every time I hear "Go is easy to learn." No. It's not. There are way too many dark corners with things lurking there to bite you. It just "looks easy" until you try to do something non-trivial. Used to love it but the more I learn about it the more I fall out of love. I can't help to wonder, would it be so successful if it weren't for all the Google-hype and the push because of kubernetes? I don't mean to bad-mouth it. It's just that I invested time and effort to what now seems to me a bad language. A good language -IMO- should get out of the way once you spend some time with it. Be as transparent as possible so that you can focus on the real problem. And do that in a safe way. But here I am in 2021 having to worry about all different types of nils. Or having to decide which way to go about marshalling JSONs. Why put at least one inefficient way to do it? I know, it makes code reviews fun. And don't get me started with all the conventions and whatnot that half the community sees as holy - if they are why not putting them in the compiler like they did with gofmt and get done with it? I mean this whole thing started off on very good premises -cut down complexity, simplify, get unproductive arguments out of the way- but somehow it looks like it stuck somewhere halfway there in a grey zone that converts it to the new java fast. No problem with java here but I guess if they wanted to create a new javaesque ecosystem/community then why go to all that trouble. So my guess is that they didn't. Dunno... Had I time and not job market restrictions I'd be looking to Elixir for concurrency and Nim for everything else. But things being what they are I'm stuck with the gopher.
- matwood 6y ago"Easy" is relative. Go has been one of the easier languages to pick up and...go. I can read most go code that isn't my own without too much trouble. Modern java is also good in this regard, and has the added advantage of so many existing libraries. Every language has warts and tradeoffs, many which have nothing to do with the language itself. Can it be hired for? Is is supported? What's the ecosystem like? How easily can it be deployed in a container?
- ronnier 6y agoTime, Enums, Final, Sets - all nightmares in Go and make me miss Java. These are the basics and they are hard or missing.