7 ms·
Introduction to Golang
- pixie_ 13y agoRandom question - but what happens when you divide by zero in go?
- motter 13y agohttp://play.golang.org/p/TjzkjvDfYa http://play.golang.org/p/TjzkjvDfYa
- mostafah 13y agoFor people not familiar with Go Playground, that’s a compiler error.
- walrus 13y agoAnd here's what it looks like at runtime: http://play.golang.org/p/bVh52aKvMi http://play.golang.org/p/bVh52aKvMi
- Dewie 13y agoDoes it only throw a compiler error if you use 0 as a constant? Let's say you have a function that returns an int; does it force you to check if that int is non-zero before using it in division?
- chimeracoder 13y agoIt can only throw a compiler error if you use a 0 literal (or constant), because it cannot determine the runtime value of a variable denominator at compile-time (obviously). It does not force you to check, but if you wanted to "catch" the "exception" (really, "recover from the panic"), here's how you would do that. Note that this is admittedly a somewhat unidiomatic use of recover(): http://play.golang.org/p/dAQ01dus9Y http://play.golang.org/p/dAQ01dus9Y
- millstone 13y agoWhich applies not just to integer math, but also to floating point: http://play.golang.org/p/vEmiqUyPag http://play.golang.org/p/vEmiqUyPag Division by zero in floating point is well defined and useful, and does not produce a runtime exception. So why is this a compile time error? Edit And it produces the wrong result for division by negative zero: http://play.golang.org/p/DKoVG0Vlaf http://play.golang.org/p/DKoVG0Vlaf It should be -Inf, not +Inf. Go's floating point math is whack.
- pixie_ 13y agoIs there any way to 'catch' a divide by zero panic in go?
- Queue29 13y agoSure, http://play.golang.org/p/dAQ01dus9Y http://play.golang.org/p/dAQ01dus9Y
- chimeracoder 13y agoFor others: note that that's only because Queue29 assigned 0 to a variable before using it as the denominator, so there's no way for Go to catch that at compile-time. If you divide by a literal 0, this is a compile-time error, so there's no panic (and no need to recover()): http://play.golang.org/p/E0jSDAHwtg http://play.golang.org/p/E0jSDAHwtg
- pixie_ 13y agoInteresting thanks. Of course then I was wondering what this recover() stuff was and I found an explanation here - http://blog.golang.org/defer-panic-and-recover http://blog.golang.org/defer-panic-and-recover cool stuff.
- tomku 13y agoAssuming it's not a division by constant zero that's caught at compile time, it panics. Go's panic/recover system seems conceptually similar to try/catch and exceptions, but they aren't a drop-in replacement for exceptions due to some differences in both style and execution. There's a good blog post[1] on the Go blog going into more detail about defer/panic/recover, if you're interested. [1]: http://blog.golang.org/defer-panic-and-recover http://blog.golang.org/defer-panic-and-recover
- programminggeek 13y agoI've just recently started playing with go and I think the most impressive part is how close you can get to a dynamic language development feel without sacrificing the benefits of static compilation, speed, binary distribution, etc. There is a lot to love about go and it is very quickly starting to look a lot more interesting than other languages like ruby for future projects.
- coolsunglasses 13y agoAnd for people who want to "level up" their programming skills and get an excellent language for writing servers in with a great faculty for safely concurrent semantics: http://clojure-doc.org/articles/content.html#clojure_tutorials http://clojure-doc.org/articles/content.html#clojure_tutoria...
- defen 13y agoHow is deployment with clojure? I haven't used either, but deploying golang programs looks incredibly simple, whereas JVM wrangling (which I last did years ago) was never fun for me. Does either one support erlang-style hot code swapping?
- theatrus2 13y agoJVM deployment is a little more involved, but due to the use of the virtual machine, can allow for hot code swaps. However, this isn't cleanly baked into many application architectures, so YMMV.
- coolsunglasses 13y agoYou might be over-rating the usefulness of code hot-swaps in production with Erlang. Erlang devs I've talked to have looked a little green in the gills when I asked about it. Coming from Python and Ruby, being able to just deploy a jar is a dream.
- drdaeman 13y agoPython has packages ("eggs", although the term has multiple meanings, I'm unaware of good unambiguous name to things you host on PyPI or alikes). You put your code under a certain conventional directory structure, write simple setup.py file and, well, that's about it. (Although, as I happen to use mostly Debian-based systems, I prefer not using Python packages directly but build .deb out of them with stdeb and use system package manager to deploy. Just dput the built package to company's private apt repo, and Puppets pull the update.)
- JulienSchmidt 13y agohttp://i.lvme.me/4gl8uip.jpg http://i.lvme.me/4gl8uip.jpg The up-to-date Go Users page is at http://code.google.com/p/go-wiki/wiki/GoUsers http://code.google.com/p/go-wiki/wiki/GoUsers
- RamiK 13y ago"This isn’t a language for use in UIs or game clients, but it is an extremely competent language for making servers, infrastructure or parallel/distributed processing systems." Golang has a GUI tool kit: the "net/http" and "html/template" packages. While I personally hate EcmaScript with a passion, I have to admit every single GUI tool kit or windowing system I've worked with had serious fundamental design flaws not unlike JavaScript and the DOM. The entire scenario is not unlike the Tcl\Tk days. People will bitch and whine but will do so while using it until something better comes up.
- pkaler 13y ago> Golang has a GUI tool kit: the "net/http" and "html/template" packages. The article is written from the point of view of a game developer. He means UIKit or WPF. Most games used to write their own UI controls.
- mrschwabe 13y agoThere sure has been a ton of Golang hype lately. At the risk of being downvoted, just wanted to add my 2 cents: seems obvious to me that Google's PR team has been in full motion on Go lately. At the very least, there are quite a few Google fans active on HN routinely pushing Go. Which is no problem of course. I just find this somewhat frustrating given that we also have the massive NSA surveillance scandal unfolding. Because I'd like to think that intelligent, free-thinking hackers would opt for using products & technologies from companies & organizations not so directly, deeply, and intricately involved in said scandal. Anyway, kudos to Dan for his detailed article - I'm sure he could care less about the political alignment of the corporation he is getting his tools from - and really it should be about technology first; so if Golang fits his needs most optimally then by all means that's what he should be using. Just saying, don't forget about the alternatives ;)
- tiziano88 13y agoMy understanding is that Go is an open source project, of which the main maintainers are employed full-time by Google specifically in order to improve the language and make it more useful and used within the company itself.
- mrschwabe 13y agoSure, perhaps it could be said then that Go is similar to that of Android. Which is the prime example of one of Google's 'open' projects that has done wonders for its' industry. Yet given the NSA's surveillance scandal and Google's long history of a close & secretive working relationship with the NSA - today perhaps we should be questioning whether or not we are still comfortable using an Android device. Or programming with Go.
- drivebyacct2 13y agoThere's this as well, for those of you with showdead http://www.appscale.com/ http://www.appscale.com/
- nolok 13y agoFor people who use go in production; what do you use for database abstraction and mapping ? I found several "ORM" but either they throw static typing out the window, or they seem to be way overblown in what they try to do (ending up dictating how some of the data has to look in the database), I look only for a database abstraction (which db/sql doesn't provide), does a good one exists ?
- drdaeman 13y agoTried the language recently. Wanted a in-memory data structure that could represent a filesystem-like graph. Ran away when I understood (please correct me if I got that wrong, but I wasted a whole weekend and found nothing) that it's either totally type-unsafe void*^H^W interface{} all the way or I have to write my own separate implementation for every and each type of graph I'll use, without any significant way to reuse code.
- cdoxsey 13y agoWithout generics you'll need to use an alternative approach. As you said you can use interface{} everywhere. Another option is to create an interface that does what you need. Checkout "sort" for an example: http://golang.org/pkg/sort/#Interface http://golang.org/pkg/sort/#Interface.
- coldtea 13y agoI think he already covered both, the second with "or I have to write my own separate implementation for every and each type of graph I'll use, without any significant way to reuse code".
- dubbledidu 13y agoSort is just one instance where one can work around Go's limitations, this is not repeatable in general.
- drdaeman 13y agoSure, I can `type Tree struct { Child []*Tree; Node NodeType }`, where `NodeType` is either `interface{}` (makes `Tree` reusable by sacrificing type safety) or some concrete type (sacrifices `Tree`'s reuse) but that's about how far I can progress. Also, I have to manually ensure that only one of Child or Node is set, but not both. I just can't do anything like `data Tree a = Leaf a | Node [Tree a]` there. (Edit: okay, my mind swayed a bit, that was trees, not graphs. I had different ideas on data structure to use, and my memories are a bit messy. Sorry about this. The problem should still hold, it applies to both trees and graphs, and I think many other data structures too.)
- catnaroek 13y agoTaken from the article: > "Strongly typed (with dyamic casting)" > "while Go is statically typed, it has a strong system for dynamic casting and reflection". This is not even wrong. The whole point to using types is to make strong guarantees about the meaning of programs without even having to run them. Dynamic casting and reflection destroy the usefulness of these guarantees, by virtue of their semantics depending on information only avaluable at runtime. Strong typing with dynamic casting and reflection is like a safe knife whose handle is a blade! > "Really good at concurrent stuff, pretty fast" Give me a break. Concurrency support is all about compartmentalizing the use of resources as much as possible, reducing sharing between the concurrent units of computation (processes, tasks, goroutines, whatever you want to call them) to the bare minimum required for the program as a whole to serve its goal. How does Go's shared memory model help in this regard? This fundamental inadequacy has negative consequences both for correctness (synchronization is a convention, it is not actually enforced) and for performance (Go needs a stop-the-world garbage collector). === Go might have been barely interesting ten or fifteen years ago, but in 2013, this kind of design has to be rejected as mediocre.
- azth 13y agoI don't know why you're being down voted. Your points are completely valid. Without Google's name behind it, Go would not have had all this hype around it.
- catnaroek 13y agoFanbois hate it when someone points at the flaws in their favorite language. I still cannot comprehend why. As much as I like Haskell and Standard ML, if someone pointed out their flaws (Haskell: lame module system, needing arcane hacks to selectively introduce strictness; SML: no open sum types, no applicative functors even when generative ones do not make sense), I would not take issue with that. Realizing our tools are not perfect is the first step to improving them. I have nothing against Google, but their programming languages (at least, the ones that I know of), Dart and Go, are really mediocre and uninspiring. For all their bad reputation, Microsoft at least has F# and is indirectly involved in the development of Haskell. Even Microsoft's take at an objected-oriented language for the uneducated masses, C#, is much better than the Google- and Apple-endorsed alternatives (Java and Objective-C).