As some gophers may have heard, Go 2 may be coming soon! It's still in the planning phase though, and the Go team is still working on what features should and shouldn't be in the language.
One of my favorite things about Go is its lack of syntactic sugar, it really feels like I'm problem solving, rather than writing tons of gibberish. The Go team doesn't want to have yet another feature-bloated language, which is why I really enjoy it.
Anyway, there's over 7 pages of Go 2 proposals, and I've been searching through them, so I thought I'd list some of my favorites.
Interface Literals #25860
This one is a bit hard to understand at first, but once you understand it, it becomes much simpler to read.
So let's say you want to sort a slice of strings... This is now simplified with
sort.Slice, but for the sake of example, let's pretend
sort.Sort is all we're able to use. This function takes an interface (called
sort.Interface) which has 3 methods defined, so in order to use it, you must make sure those 3 methods are defined for your type:
But with this proposal, we can make it look like this instead!
Essentially what we have done is to use an interface literal instead of needing to make a new type! This means that the code that you use to sort can now be put where you actually do your sorting.
collect statements to the language #25626
This one is really nice, actually. They want to add a statement called
collect to the language. It's hard to explain with words (in my opinion), so let's do it with an example!
Here is some valid Go code, with some error handling of course:
And as my Computer Science professor said, if you're repeating code, you're doing it wrong! But in Go, there's no good way to really handle the
err != nil repetition problem here, so how could we help this? The
collect statement helps fix this issue!
So let's have the same code, but with the
So what does this do? Well the
collect err indicates that we are going to collect into a variable
err. Then, each time we assign to
_!, it assigns that value to
err, and checks
if err != nil. If the assigned value is not nil, it exits the
collect statement, and then the error handling can be done afterward.
My main issue with the proposal is that the syntax is a bit confusing (I personally don't like the
_! syntax, I made a suggestion to use
err! instead, which makes it a bit more clear).
Fix casing issues #1187
This one is quite a bit simpler than the last one. The
os package has a lot of confusing casing, it's not always clear if the correct function call is
os.ChDir, or if it should be
os.TempDir. I'm hoping the proposal to fix these casing issues gets accepted!
collect statement, I'm not sure if the Go creators would like it, as it may be seen as an unnecessary feature (which I understand).
What do you think about the proposals? Do you have any you want to add? Let me know, Go is one of my favorite languages out there, and I'd love to have more insight as to what its future may look like!