Comment by NateDad
11 years ago
You can do that in go with just a few more characters:
g := some_input() // A list with 3 elements
a, b, c := g[0], g[1], g[2]
I'll grant you, having to specify the indices is slightly more verbose, but it's also a lot more clear... because what happens when g has more or less than 3 elements? In Go it's clear, more is ok, less will get you a panic.
lst := []int{1, 2, 3, 4}
head, tail := lst[0], lst[1:]
init, last := lst[:len(lst)-1], lst[len(lst)-1]
a, b, c := lst[0], lst[1:3], lst[4]
Again, I don't think the magic unpacking is really helping that much... doing it the same way you'd do anything else in Go is slightly more verbose, but it's also not some new syntax you have to figure out either.
No comments yet
Contribute on Hacker News ↗