Live data from Hacker News

a[low:high:max] in Golang – A Rare Slice Trick

build-your-own.org

111–120 of 131 posts

Re: a[low:high:max] in Golang – A Rare Slice Trick

#111
post #26

My fave golang slice trick is the len of an empty slice is 0, but the slice itself is == to nil, but the len of nil won't compile. Can't understand that one. https://go.dev/play/p/MslCkBphl7q?v=gotip

Because the bare value `nil` has no type. Typing it, e.g. `len([]int(nil))` works fine.

This is the most satisfying answer, but it doesn't make the implications less complex.

Re: a[low:high:max] in Golang – A Rare Slice Trick

#112
post #111

Earlier quoted context omitted.

Because the bare value `nil` has no type. Typing it, e.g. `len([]int(nil))` works fine.

This is the most satisfying answer, but it doesn't make the implications less complex.

What implications did you have in mind?

In languages with null and type inference, `var x = null` is probably not going to infer the type you want. In languages with function overloading (which is essentially the case for Go `len`), `f(null)` is going to be a compile-time error if multiple overloads are potentially null.

Re: a[low:high:max] in Golang – A Rare Slice Trick

#113

Earlier quoted context omitted.

TLDR: The best part of Go is that there is very little magic in Go. If you understand that slices are just fat pointers implemented as a built-in, there is nothing confusing about them. I can understand every part of a Go program, all the way down to the language syntax that generate assembly. I don't have to be afraid of or be mystified by any language feature, because 1) there are few, 2) they are just programs imp…

Why not just use c at that point?

probably because it's simpler and there are fewer things to worry about. few write their web app in C.

Re: a[low:high:max] in Golang – A Rare Slice Trick

#114
post #64

why do people write articles about go features? when PHP was in its prime, almost nobody wrote blogs explaining how they found some philosophy in PHP. there's a reason for that. when you see someone open their article explaining a language feature by talking of the implementation details or specific use cases, that's a language smell (of course all industrial PLs stink). ironically go is the only post 80s language th…

I'm not sure how your snippet above exemplifies memory unsafety. Concurrent access does let you hit some 'fun' behavior, but you have to be doing pretty dumb things to hit them. And while the implementation may be able to save you from something like that, such things would likely bubble up elsewhere(disk i/o, network i/o, etc) if doing that kind of thing.

I would also consider this to be memory unsafe. If you have an "array", you should not be able to index (or slice) beyond its bounds. If you are allowed to do so, you may have unpredictable junk in your array.

Re: a[low:high:max] in Golang – A Rare Slice Trick

#115

Earlier quoted context omitted.

> The best part of Go is that there is very little magic in Go. If you understand that slices are just fat pointers implemented as a built-in, there is nothing confusing about them. It's just a tautology. If you understand something, of course by definition you aren't confuse about them. By using the same logic, all languages have "very little magic."

But the big difference is time to understand it

Offloading that pain of learning as the pain of using.

Re: a[low:high:max] in Golang – A Rare Slice Trick

#116
post #26

My fave golang slice trick is the len of an empty slice is 0, but the slice itself is == to nil, but the len of nil won't compile. Can't understand that one. https://go.dev/play/p/MslCkBphl7q?v=gotip

Empty slices also serialize to `null` instead of `[]` when using the default json encoder.

Re: a[low:high:max] in Golang – A Rare Slice Trick

#117
post #93

Earlier quoted context omitted.

"Proto" meaning protobuffers?

I think it rather "communication via some protocol" in general. In other words if we are not talking about giant monolith - we have N small systems to begin with.

I meant specifically protobuffers in that case, but sure, that works.

Re: a[low:high:max] in Golang – A Rare Slice Trick

#118
post #111

Earlier quoted context omitted.

This is the most satisfying answer, but it doesn't make the implications less complex.

What implications did you have in mind? In languages with null and type inference, `var x = null` is probably not going to infer the type you want. In languages with function overloading (which is essentially the case for Go `len`), `f(null)` is going to be a compile-time error if multiple overloads are potentially null.

the ability to cast nil as a zero length array means there's no difference between a function that returns a zero length array and a function that returns nil (assuming some casting process takes place). It could be a subtle and annoying bug to track down the difference.

Re: a[low:high:max] in Golang – A Rare Slice Trick

#119
post #118

Earlier quoted context omitted.

What implications did you have in mind? In languages with null and type inference, `var x = null` is probably not going to infer the type you want. In languages with function overloading (which is essentially the case for Go `len`), `f(null)` is going to be a compile-time error if multiple overloads are potentially null.

the ability to cast nil as a zero length array means there's no difference between a function that returns a zero length array and a function that returns nil (assuming some casting process takes place). It could be a subtle and annoying bug to track down the difference.

nil isn't cast to an empty slice (Go doesn't have casts, except maybe the new pointer-to-array syntax if you want to count that), nil is the default value of a slice, and that value is also empty. Other empty slices may be non-nil, because they may have capacity, or have been sliced out of another buffer, etc.

Of the various legitimate issues around nil (box vs. unboxed, nil receivers, nilability of all pointers), this is the most not-actually-ever-an-issue.

Re: a[low:high:max] in Golang – A Rare Slice Trick

#120

Earlier quoted context omitted.

I'm not sure how your snippet above exemplifies memory unsafety. Concurrent access does let you hit some 'fun' behavior, but you have to be doing pretty dumb things to hit them. And while the implementation may be able to save you from something like that, such things would likely bubble up elsewhere(disk i/o, network i/o, etc) if doing that kind of thing.

I would also consider this to be memory unsafe. If you have an "array", you should not be able to index (or slice) beyond its bounds. If you are allowed to do so, you may have unpredictable junk in your array.

This is not any usual definition of memory-unsafety; the contents may be useless to your task at hand but it's well-defined.
Post reply on HN