41 ms·
Go Replaces Interface{} with 'Any'
- twsted 5y agoRight decision, in my opinion. I was hoping this.
- Animats 5y agoThat's so Visual Basic. Not a bad move, though. Most Go "Interface" things do tend to be "{}"
- RunOutOfMemory 5y agoWhat about struct{}?
- ibraheemdev 5y agoWhat about it?
- RunOutOfMemory 5y agointerface{} -> any struct{} -> ?
- lalaithion 5y agoI suppose you could define a type alias "unit". It's not surprising to me that this isn't in the go standard library though.
- benhoyt 5y agoYou can define that yourself as "type token = struct{}" or similar. In my experience struct{} occurs even less than interface{}, and it's also 3 characters shorter already, so it's not worth a builtin. I use "chan struct{}" for synchronizing and sometimes use "map[string]struct{}" for sets.
- ainar-g 5y agoWe use the alias "unit", like lalaithion has said before, in many projects. It's a bit annoying to declare it in every package though. Hopefully, there'll be an official alias in the future.
- deleted 5y ago[deleted]
- smokey_circles 5y agowell it's been a good few years, but now it's time to pick up another language and wait for the community to beat stupid ideas into that one too
- mseepgood 5y agoIt's a type alias, introduced for generics. By the way, Go 1.18 Beta 1 is released (with generics): https://groups.google.com/g/golang-announce/c/eAjK4Oezs_A https://groups.google.com/g/golang-announce/c/eAjK4Oezs_A
- exdsq 5y agoHow is generics not a big enough change to warrant 2.0? Edit: I’m impressed generics aren’t a breaking change! I thought this changed Interface{} to Any as a breaking change
- preseinger 5y agoIt's not a breaking change.
- mseepgood 5y agoThey are backwards compatible and don't break people's code, so it's 1.x.
- philwelch 5y agoIn semantic versioning, going from 1.X to 2.X is only indicated when there are incompatible API changes. Adding generics doesn’t break compatibility with preexisting Go code, so it’s unnecessary to increment the major version.
- keithnz 5y agoSure, but semantic versioning really is the wrong kind of versioning to use for a language. The major version should represent major language changes, not whether its a breaking change or not, semantic versioning isn't somehow magically a "good" way to version. It's useful for libraries / dependencies where you are dealing with many different libraries and just want to know you can upgrade without having to deal with breaking changes. For a language? Silliness. Your version is not really telling you the main things you care about. It's much much more useful to the users to say, 2.0 introduced generics, it's distinct. If it's like other languages, generics changes the code people generate a lot, libraries start looking significantly different. It's very distinct, and if that is simply in version 1.18.0 or whatever, that is super bad usability from a language perspective.
- nkozyra 5y agoI like it, interface{} always felt unlike anything else in go other than structs.
- mseepgood 5y agoWhy does 'interface{}' feel unlike 'interface{String() string}' to you? Just because it doesn't have a method? You know that you can inline non-empty interfaces, too? func foo(x interface{String() string}) string { return x.String() }
- xmprt 5y agoI think people agree that those look similar however the problem is that the first is a common practice while the latter is something that no one in their right mind would do. If people replace the second with something like Stringer, then the first should be replaced too.
- nkozyra 5y agoI think you (and the other reply) are for some reason confusing my complaint as functional rather than purely syntactical.
- throwaway894345 5y ago`interface{}` is just an empty interface, and every type implements `interface{}` because every type has at least zero methods.
- baby 5y agoThis is great, as much as interface{} is not great, it's still part of the language and really verbose/non-descriptive. I'm guessing any was a forbidden word?
- mseepgood 5y ago> I'm guessing any was a forbidden word? You mean a reserved keyword? No, 'any' is not a keyword, it's an identifier (a predeclared identifier). The usual shadowing rules apply.
- shadowgovt 5y agoIt wasn't so much that `any` was forbidden as that `interface{}` practically meaning "any value" fell naturally out of Go's existing type system rules, so there was no need to introduce another concept. But having `any` as syntactic sugar for that concept is a good idea.
- uberman 5y agoI love this change/alias
- jamespwilliams 5y agoLoading this commit in safari almost killed my phone
- jeffbee 5y agoThe Gerrit view of the same commit transfers far less data than the GitHub view. https://go-review.googlesource.com/c/go/+/368254 https://go-review.googlesource.com/c/go/+/368254
- hu3 5y agoWorks fine for Firefox on a Pixel 6. Took a couple of seconds to load then smooth (ginormous) scrolling was available. Same for an old Android phone with outdated Snapdragon 660 I use to test app performance bottlenecks. Except this one took 4 secs to load.
- saagarjha 5y agoLoaded just fine on my iPhone SE. I hope you’re not using anything older than that…
- stordoff 5y agoLimiting a phone's usable life to about five years feels somewhat arbitrary, and there are iPhones older than the (original) SE that are still getting security updates (5S/6).
- maxk42 5y agoSo now that go has generics and modules, is there an up-to-date intro for writing cutting-edge Go code for people who are already pretty familiar with the older styles?
- makeworld 5y agoI wrote a tutorial on Go Modules, hope it's helpful. https://www.makeworld.space/2020/11/go_modules.html https://www.makeworld.space/2020/11/go_modules.html Looking for a tutorial on generics myself.
- mseepgood 5y agoThere is lots of tutorial-style documentation on modules: https://go.dev/doc/#developing-modules https://go.dev/doc/#developing-modules There is also a tutorial on generics: https://go.dev/doc/tutorial/generics https://go.dev/doc/tutorial/generics
- maxk42 5y agoI tried doing the modules tutorial a while back but had difficulty with the directory structure. At some point I think the "recommended" directory structure became mandatory and I don't think my personal dev environment ever quite lined-up with it. Perhaps I should wait another year then buy a book.
- mseepgood 5y agoModules don't require any particular directory structure, just a go.mod at the root of your project.
- hamburglar 5y agoThat’s “a particular directory structure” that conflicts with many already-existing projects. What if I have a mostly-java project that has a couple bits of go code buried in it that I want to use as modules? Break it up into multiple repos for no good reason? No single tool should be acting like it owns the root directory of my repo.
- arcanon 5y agoBall is in your court now typescript-on-the-serverside ppl!
- jamespwilliams 5y agoI’m not a typescript person, but to be fair to typescript, its type system is still much more expressive than Go’s, even with the generics addition
- woah 5y agoTypescript is stricter and safer while also being less obtrusive than Go
- travisd 5y agoThis isn't super clearcut. Typescript has lots of footguns and famously says that soundness is not one of their driving factors. There are lots of ways to devolve to accidental implicit any's (even with the no implicit any strict rule turned on). Of course, Go makes zero attempt to enforce nullability (nilability?) in its type system... so... win some, lose some I guess.
- Thaxll 5y agoAny language based on JS can't be safer than Go. The end result of TS is JS which is a dynamic language.
- friedman23 5y agoOk, so if Go compiled to JS it would be a dynamic language? https://github.com/gopherjs/gopherjs https://github.com/gopherjs/gopherjs
- ByteJockey 5y agoUltimately everything ends up as machine code, which is untyped.
- maxpert 5y agoI promised myself I will stay patient, but I think I am on verge... It's everything I've dreamed off!
- rackjack 5y agoI'd really like it if Github handled massive commits like this one better. The way they present it is not very pleasant to navigate.
- gepoch 5y agoSame.. This is just a mirror though, so at least you can browse it more sanely over on Gerrit. https://go-review.googlesource.com/c/go/+/368254 https://go-review.googlesource.com/c/go/+/368254
- kbd 5y agoThis is fantastic. It'll make Go feel much less weird. eg from the diff: []interface{}{1, 2.0, "hi"} -> []any{1, 2.0, "hi"} Now that Go is going to have generics, all we need is sugar syntax for early return on error -- like more modern languages such as Rust and Zig have -- and Go may finally be pleasant to program in!
- linkdd 5y agoI cannot wait for the Result monad. The state of error handling in Go at the moment is embarrassing at best.
- papito 5y agoPeople downvote it, but it's true. 2/3 of your Go code is `if err` blocks. And the way you have to chain the error messages to make the stack make any kind of sense is just maddening.
- Vanclief 5y agoI used to really hate that. After reading this amazing post https://middlemost.com/failure-is-your-domain https://middlemost.com/failure-is-your-domain I created a module that makes this bearable https://github.com/Vanclief/ez https://github.com/Vanclief/ez
- neop1x 5y agoWe often have errors handled. In contrast to many Java codes just spitting unhandled exception stacktraces or crashing completely.
- papito 5y agoYou must handle an exception in a language like Java. Yes, you can just print the stacktrace, but that is a choice. I don't HAVE to handle errors in Go until the program crashes as well. I never really understood that argument.
- 5y ago
- deleted 5y ago[deleted]
- moth-fuzz 5y agoI'm not sure I like this change. I liked interface{} since it just works out naturally from Go's relatively simple type system, and anyone could come to the conclusion without actually being told "use interface{} to represent any possible value" just by having an understanding of that type system. Adding what is simply a type alias, multiple words for the same underlying concept, to me just feels like jargon. I admire Go for its simplicity and clarity and this change gives me anxiety that that will be obfuscated.
- eadmund 5y agoI agree with you right now, but I wonder if I will change my mind after having had some experience using the new generics. Maybe it will feel more natural then?
- takeda 5y agoIsn't Any essentially: type Any interface{} ?
- deleted 5y ago[deleted]
- erdaniels 5y agoIt's a type alias so it's type any = interface{} That means they're interchangeable compared to defining a new type.
- Laremere 5y agoClose, it's: type Any = interface{} That is, it's an alias and casting is not necessary.
- throwaway894345 5y agoI'm pretty sure it's an alias, so: `type any = interface{}`. `type any interface{}` would declare a new type, while an alias is exactly another name for the same type.
- xh-dude 5y ago
- AtNightWeCode 5y agoLike it! Now, can they continue to add more “useless” improvements like while-loops and so on.
- jjice 5y agoDoesn't 'for' work as 'while' if you only give it a condition in go? I haven't written much, so correct me if I'm wrong.
- AtNightWeCode 5y agoDoes not Interface{} work as well as Any? I mean, most languages have while-statements cause it is more clear what the intention is.
- Zancarius 5y agoIt does, but with generics coming down the pipe, I feel that "any" expresses intent somewhat better in certain circumstances. Or at least it ought to improve readability-at-a-glance. I'd imagine you can continue using interface{} for the foreseeable future.
- Someone 5y agoSome languages even have real for statements because it makes more clear what the intention is. In Pascal, you can’t break out of a for statement, the limit and step value are evaluated only at start of the loop, and you can’t change the value of the variable being looped over. Consequently, the number of iterations through the loop taken is known when the loop gets entered.
- xh-dude 5y agoOTOH there’s no ‘while true’ or do-while or while-do, none of which are particularly fluent.
- ainar-g 5y agoIt does and it's enough for "while". What I do miss sometimes is the repeat-until / do-while loop. You can emulate it with "for" currently, but it's awkward.
- alkonaut 5y agoAfter generics (coming soon), a proper sum type and then I'll shut up.
- qaq 5y agosame :) maybe also relax rules a bit to allow type elision for structs in function calls to get python like keyword args. foo("blah", {option1: true}) would work better than functional options people use now.
- 13415 5y agoI think they should freeze the language for the next 10 years or so.
- qaq 5y agoafter above 2 changes sure :)
- collinvandyck76 5y agoThis would be lovely
- erik_seaberg 5y agoI just want to write f(g(), h()) without nine lines of useless noise that editors don’t know how to hide. I guess it might be possible to write a generic function that uses panic/defer to return an error from a subexpression.
- zmj 5y agoNobody woken up in the middle of the night to debug your code wants you to write that.
- brabel 5y agoTheir code is full of things like: type fileOps []any // []T where T is (string | int64) Go does not have neither generics nor union types. So people have to do this kind of thing :( I feel sorry for them. Reminds of Java 4 (15 years ago or something) where code was full of this crap: List /* <String> */ values; Map /* <String, Object> */ map; Some devs spent a whole week doing nothing other than removing those commented out generic type declarations once Java finally got generics!
- mseepgood 5y agoYou're lucky: Go 1.18 Beta 1 was released today, which has generics.
- mikojan 5y agoCraazzyyyyy - ty!
- Thaxll 5y agoGo has generics in that commit.
- qaq 5y ago1.18 Beta is out today with Generics
- gameswithgo 5y agoYou could have sum types, and not generics to cover many of these cases I guess? And then Go could have the error type it kind of wants to have.
- amelius 5y ago> Some devs spent a whole week doing nothing other than removing those commented out generic type declarations once Java finally got generics! Fast-forwarding to today, could Co-pilot have saved them the trouble?
- tjalfi 5y ago
- welder 5y agoGood, now we just need the ability to declare function parameters and return values non-nullable (Forbid passing nil into a function, and declare a function will never return nil). That would get rid of the "panic: runtime error: invalid memory address or nil pointer dereference" errors. https://wakatime.com/blog/48-go-desperately-needs-nil-safe-types https://wakatime.com/blog/48-go-desperately-needs-nil-safe-t...
- jchw 5y agoI’d kill for union types as well. … and pattern matching. Maybe just some extensions for `switch`. … and one of the `try` proposals. That having been said… I do appreciate that Go has gotten where it is today by being radically simple, and that a lot of extreme care needs to be done to add new features to the language. It’s hard to draw a firm line in the sand. I feel like all of these features would work great together, though; it’d enable Go to do something like `Result` in Rust with few language-level changes. I even remain somewhat skeptical about generics, but I am hopeful.
- halfmatthalfcat 5y agoAt that point, let's just write Scala.
- noisy_boy 5y agoI think a language that doesn't focus much on backwards compatibility isn't a great comparison to Go :) Having written some Scala, I really like the language. I also like Go's excellent performance, easy concurrency, ability to produce a single binary without having to rely on JVM etc. I think it would be a bad idea to get overly complicated constructs into Go but union types/pattern matching are relatively accessible, not uncommon now and provide a lot of expressiveness without the risk of code becoming unreadable. Those combined with maybe some alternative approaches for error handling will make me want to write Go again (I've written one component in Go that is working perfectly but it was just too much code with bulk of it being err != nil).
- 5y ago
- Ericson2314 5y agoC++, Java, JavaScript, Python, Ruby, Go, now all with popular and/or official type checkers which support generic. The normie PL bar is rising, slowly but surely.
- skohan 5y agoNext let’s do ADT’s
- Ericson2314 5y agoIndeed! C++ got std::variant, Python got....some sort of pattern matching, so there's hope?
- notthathardbro 5y agoIt's fun to imagine trying to wander into a Go community ~4 years ago and suggest that this might happen. I actually feel small and withdrawn just thinking about it. Boy, I still remember when I go shit on by Real Go Devs who insisted there was never a need for more than one GOPATH or real dep management. And raged when I gave countless examples of how large Go projects (cough k8s) were doing all sorts of ... creative stuff... to accomodate The Google Way.
- throwawaylolz 5y agoHackerNews on TypeScript: "Like OMG we need to get rid of Any, it is such a catch all bullshit workaround for a very serious problem, I have developed billions of applications across every domain and like OMFG this is garbage, THIS IS NOT TYPED, WE SIMPLY NEED TO REMOVE ANY AS IT MEANS ANY GARBAGE AND KIDS JUST USE IT". HackerNews on Go (6 years later): "AT LAST! WOOOO HOOO ANY, WOW" Me: "what?" HackerNews: "Go" Me: "OK?" HackerNews: "Yes"
- blahgeek 5y agogofmt -w -r 'interface{} -> any' src Is this a real command? If so, I’m very impressed. Is there any equivalent for c++ and other languages?
- linkdd 5y agohttps://linux.die.net/man/1/sed https://linux.die.net/man/1/sed maybe?
- zmj 5y agoYep, it's a rewrite rule: https://pkg.go.dev/cmd/gofmt https://pkg.go.dev/cmd/gofmt The combination of unambiguous syntax and consistently-formatted code results in rewrites producing meaningful diffs (most of the time).
- jamincan 5y ago'cargo fix' will upgrade your code from one Rust edition to the next as well as apply corrections for lint warnings.
- ainar-g 5y agocargo fix is closer to (and possibly inspired by?) go fix[1], imo. [1]: https://pkg.go.dev/cmd/go#hdr-Update_packages_to_use_new_APIs https://pkg.go.dev/cmd/go#hdr-Update_packages_to_use_new_API...
- tjalfi 5y agoIntelliJ, Resharper, and presumably Rider have structural search and replace[0]. It's available for at least C#, Java, and Kotlin. [0] https://www.jetbrains.com/help/idea/structural-search-and-replace.html https://www.jetbrains.com/help/idea/structural-search-and-re...
- Calamitous 5y agoOh, that's nice. :)
- onelovetwo 5y agoLove generics in Swift, this will be great
- malandrew 5y agoIn related news, does anyone have any idea when a new edition of Kernighan and Donavan's gopl will be released to include all the changes since 2015? https://www.gopl.io/ https://www.gopl.io/
- malandrew 5y agoEmailed the authors. No plans right now but Donavan talked about the big year of changes ahead, so I'm guessing sometime after the dust has settled from changes in 2022.
- tex0 5y agoNow the question is when it's available with GitHub Actions.
- ogogmad 5y agoGoing to be downvoted for this, but it would be cool if Go had operator overloading. That way, you could write matrix code in it. I think if Go did this, the language could end up being used in computer graphics and Deep Learning. Oh, and maybe this goes against the Go ethos, but Python has a library called Sympy for Computer Algebra, and it's quite nice. It clearly relies on operator overloading. I'm guessing this won't happen for a long time because operator overloading has a reputation (rightly or wrongly) for being abused. Oh well! [edit] More philosophical point: Overloading in programming started with mathematical operators, no? The motivation for having it as a language feature is very clear (if you know enough mathematics). What happened?
- kwhitefoot 5y ago> if Go did this, the language could end up being used in computer graphics and Deep Learning. People have written programs in both of those areas using Fortran-IV which doesn't have overloading either.
- ogogmad 5y agoI think your comment is designed to troll me. The fact that it's so short and arrogant makes this more likely to be true. Just to be clear: Deep Learning means ConvNets or something fancier, which only became a hot research topic circa 2010 (though they also existed as a prototype in the late '80s). I doubt anybody has programmed ConvNets in Fortran IV; a language from 1962. And even if they did program a prototypical form of ConvNets (I suppose because they were originally an obscure proposal from the '80s) in Fortran-IV, it wouldn't be able to use such essentials as the GPU, or convenience features like autodiff, so in today's world it would be a toy. I suppose I've fed the troll.
- Cthulhu_ 5y agoI hear what you're saying, but why make one language look like another when that other language is more suitable for those purposes (graphics and deep learning)? Isn't Python the go-to language for deep learning? It's probably better to stick to that one, because the field itself is hard enough already - learning another language on top will just put people off.
- tgiba 5y agoLove this change!
- jangritech 5y agoJangritech is a technology website here you will find information about upcoming mobiles, laptops with specifications and descriptions. https://jangritech.in/ https://jangritech.in/
- unixhero 5y agoIn Raku/Perl6 these things can be renamed and shaped on the fly. I think that is an amazing capability of a language. Its a little bit mind boggling to me.
- popitter 5y agoThis [1] publication argues that `interface{}` should be replaced with `yolo`. [1] Cornelius Diekmann, “Looping with Untyped Lambda Calculus in Python and Go,” Paged Out!, vol. 2, p. 22, Nov. 2019. https://pagedout.institute/ https://pagedout.institute/ https://www.net.in.tum.de/fileadmin/bibtex/publications/papers/diekmann2019pagedout.pdf https://www.net.in.tum.de/fileadmin/bibtex/publications/pape...