13 ms·
My Opinionated Guide To Go
- sergiotapia 12y agoI just wish Go would let me compile and run with a flag -allowing- unused imports. I find myself wasting a lot of time hunting down unused imports or variables and commenting them out. Maybe that'll be a non-issue the more I use Go, but so far it's a bit of a time waster.
- AYBABTME 12y agohttps://github.com/bradfitz/goimports https://github.com/bradfitz/goimports
- artursapek 12y agoExactly. I have vim handle it for me every time I save. No problemo.
- krat0sprakhar 12y agoCan you elaborate on how you are doing this? Would love to add this to Vim. Thanks!
- kevrone 12y agoThere are some instructions in the docs for this: https://godoc.org/code.google.com/p/go.tools/cmd/goimports https://godoc.org/code.google.com/p/go.tools/cmd/goimports For sublime users, see here: http://michaelwhatcott.com/gosublime-goimports/ http://michaelwhatcott.com/gosublime-goimports/ It's a great tool, very convenient.
- artursapek 12y agoOne of the things I love most about Go is how everything you need comes with the language, unlike many language ecosystems where you have to depend on third parties for profilers, plugins, etc.
- rmcpherson 12y agoTry https://github.com/fatih/vim-go https://github.com/fatih/vim-go. It adds seamless support for all the go tooling to vim, including go-imports.
- jerf 12y agoTo explain to those who may not know what's going on here: Go forbids importing modules that you then don't use in the source code. This is particularly annoying with the "fmt" module, which contains things like Printf that are useful for debugging, but you may only be using fmt.Printf for one debugging statement in the code. Consequently, if you're developing, and you're adding and removing it over and over you also have to add and remove it to and from your import list over and over, which is very annoying since it's likely to be relatively distant from your use location, and it's mandatory that all imports are listed at the top of the file. (So, you can't do what you can do in perl and say "use Data::Dumper; print Dumper($debugging_stuff);" all on one line, without even being concerned about whether Dumper may already be imported.) goimports is a source filter that cleans out any unused modules, and tries its best to add modules that you only reference. It's pretty good. You can confuse it if you have two modules with the same last bit of the name, but the standard library doesn't have that anyhow, and for the most part you can deal with that. Since it also runs gofmt for you on save, it's also a drop-in replacement for gofmt, which you should configure your editor to run on every save even if you for some reason don't want the goimports functionality. (Seriously. Just do it. There's no excuse not to. There's no excuse to ever commit code that was not gofmt'ed.) If you do that, the problem goes away. Typing "fmt.Printf" pulls fmt in automatically, removing the one line removes it automatically, the import list is always accurate, and it's smart enough that if it isn't the only usage it doesn't remove it. That said, I also strongly recommend setting up automatic syntax checking by compilation (flymake in emacs, don't know what in vim, etc), so that you also avoid the "Save -> switch windows to terminal -> compile -> get smacked in face with an error that feels stupidly persnickety" psychological torture. If the errors highlight in your editor on save or something, you go through that much less. Those who lived in the C#/Java etc world have long had this... a lot of people coming from the scripting side would be advised to pick up a bit more of the helpful tools the static side has to offer.
- mediocregopher 12y ago> flymake in emacs, don't know what in vim https://github.com/scrooloose/syntastic https://github.com/scrooloose/syntastic for vim.
- kevrone 12y agoIt's worth mentioning that the author of this tool is Brad Fitzpatrick, one of the main contributors to the Go standard library and, in my opinion, the most "entrenched" Go developer on the team. And by that I mean he uses Go to build large and practical projects on his own (See Camlistore), so he knows what the language feels like from a perspective similar to the rest of us. He's also one of the only advocates on the team for the inclusion of generics.
- mattgreenrocks 12y agoMaybe Go is trying to tell you that the "move fast and break things" methodology is not how it should be used?
- sergiotapia 12y agoI know right? I do feel a warm fuzzy feeling when things compile and I generally feel much safer than when building things in Ruby.
- kjs3 12y agoWhen I talk to the youngsters about this exact feeling they look at me like I'm nuts. Coming from Ada/Modula-2/etc. where you put the thought in up front and if it compiled you were a long way to being "right" isn't something they get.
- mattgreenrocks 12y agoMe too. I don't mind working in a good C++ codebase. It feels a lot safer than working in Ruby where things suffer from bitrot due to a culture of new and shiny.
- rthomas6 12y agoWhy does this article only compare Go with interpreted languages at the top? Go is a compiled language; wouldn't it be more fair to compare it with other languages that can be compiled, like C++, Rust, Haskell, or Lisp/Scheme? I mention this because the article acts as if getting rid of abstraction layers between the OS and the code is a new idea. No, that's how all compiled languages are... that's the point of compiled languages. I suppose Go's most common use case lines it up against a bunch of slow interpreted languages, but really there's no reason except good libraries that you can't use the other compiled languages I mentioned for web apps.
- kasey_junk 12y agoWell to be fair it does mention Java (though the deployment model expressed is, shall we say, Byzantine).
- rthomas6 12y agoJava, while compiled, isn't compiled to machine code, and the bytecode to which it is compiled is run on a virtual machine. For this reason, I consider Java more of an interpreted language than a compiled language per se.
- pjmlp 12y agoThere are quite a few comercial JVMs with native compilers the HN crowd tends to ignore.
- kasey_junk 12y agoFine but calling it slow is unfair, especially in relation to Go.
- rthomas6 12y agoAh, true. I guess I didn't mean to call Java slow, but interpreted languages in general (at least, their reference implementations) are pretty slow. You're right... Java's kind of in its own category in a lot of ways.
- ZenoArrow 12y agoHave noted this 'opinionated' trend going for quite a while, so this comment isn't about this article in particular, but can someone break down why an opinionated guide to something technical would be preferable to an impartial one? The only thing it seems to imply is that bias adds value in its own right.
- lmm 12y agoIt's a pre-emptive disclaimer "I know this isn't objective, don't complain about that". And it tells you that the guide isn't going to offer you confusing choices, it's going to tell you one way to do things. For some people that's an advantage.
- ZenoArrow 12y agoThanks for your reply lmm. I can see what you mean. However, I do think it has a high risk of building new dogmas without the understanding of when these dogmas should be broken.
- rthomas6 12y agoI see value in educated opinions. For instance a while back I was trying to figure out how to make a web app in Python (keep in mind that, while I can program, I have zero knowledge about web things). This is what I saw: https://wiki.python.org/moin/WebFrameworks https://wiki.python.org/moin/WebFrameworks Which framework is the best? Why are there so many? Which is the easiest for my specific goals? An opinionated guide that said something like "Use Flask because X" or "Use Django because Y" would have been pretty useful.
- ZenoArrow 12y agoBeginners guides are a special case, the main focus is in getting something working with the minimum of fuss that you can understand. I do understand that expert opinions can help build confidence, but perhaps it's even better to explain matters clearly so you can make your own opinions.
- cmhamill 12y agoNot entirely on-topic, but the mention of the various language "stacks" at the beginning of the article reminded me of something I've been wondering. Why does the de facto standard for web apps in Go-land seem to be using the built-in HTTP server provided by net/http, or otherwise having the program server as its own HTTP server? Most other languages seem to have converged on FastCGI or some similar model ({W,P}SGI, Rack, etc.). It irks me a bit because I don't understand why you'd want to do it this way; it seems preferable to have a dedicated HTTP server in pretty much every way I can think of. Does anyone have any insight there?
- mwsherman 12y agoWell, I think one answer if that you can quickly have a running web app without another piece of software. This is an attractive quality of Node, too. In fact, if a language doesn’t have a “5 lines to a web app” demo, I consider it insufficient. I’d posit that 90+% of apps don’t need another http server. Big apps have good reasons for a dedicated http server – unifying SSL termination, load balancing – but they are a minority. There is nothing to to prevent you from putting nginx in front of the Go app if that’s better.
- kaoD 12y agoI guess it's because the HTTP module works out of the box and you can still have the dedicated HTTP server using a reverse proxy like Nginx.
- jksmith 12y agoCustom windows servers that do failover and interceptor pattern at the same time. Try that with IIS without pulling your hair out.
- jerf 12y ago"Most other languages seem to have converged on FastCGI or some similar model ({W,P}SGI, Rack, etc.)." The languages you are citing are slow enough that the scripting language would like to slice away as much of the brute web serving load as possible so the slow scripting language can concentrate its power on the real task at hand. Go is more on the compiled side. It's not C-fast quite yet (I expect it to eventually converge somewhere around 1.5x as fast, it's not quite there yet), but currently around 2-3x slower only (and net/http is seeing a lot of direct work on it, it's quite fast now). You don't get the big win that you get with letting nginx handle the brute string manipulation of HTTP and prechewing on it for the 20-100x slower scripting language. Also, remember that you can view HTTP itself as a form of CGI request... really, FastCGI etc. doesn't do anything that an already very multi-threading-friendly language can't do with straight HTTP. Those standards are solving a lot of problems that Go just doesn't have, and straight HTTP is actually more general and flexible if you can afford it (no problems with Nginx being unable to stream FastCGI, etc).
- paukiatwee 12y ago"Go: Code -> Operating System -> Hardware" Is there any reason to use RVM, PVM on production while Go does not required one? I believe should be install one and only one Ruby/Python version on production. Why Ruby/Python does not required application server like unicorn/wcgi, etc? The article clearly try to make other deployment more complicated.
- treystout 12y agoA lot of python shops I've come across don't use the system installed python or ruby. In python land at least it's exceedingly common to deploy a virtualenv with a separate python runtime and isolated 3rd party libs. This makes pinning your version deps easier and doesn't matter if the underlying server is ubuntu or centos or whatever.
- paukiatwee 12y agoThat is true, what if in future ubuntu/centos include Go by default (which might not the version you use)? I believe then will have some GVM or similar tool to solve the problem. Edit: As others pointed out, Go is compiled binary and does not required runtime (like JVM) so versionning is not issue. Thanks for clearing up.
- kasey_junk 12y agoNot being a Go developer I don't understand why this would be the case. Doesn't Go compile code to a binary? Why should Go even be required on the server?
- Supermighty 12y agoThere won't be a need for a GVM as Go is not a stand-alone runtime. Once compiled for a specific platform it will just run. You don't even need it installed on the server. You can cross-compile on your DEV machine and move the binary LIVE. Then it just runs. It doesn't need any libraries on the server.
- 12y ago
- icebraining 12y agoDocker -> Operating System This makes no sense. The runtime of Docker is the Operating System. There's no extra layer, as it's being implied. And there's no difference between Go and the other languages that makes Docker more or less useful. You can have separate Python processes without Docker, just like you'd have separate Go processes. I like Go, but this article just sets up strawmen to bring down.
- regularfry 12y agoThe question he's addressing isn't what you can do, but what is commonly being done. You don't need RVM, you don't need Docker. Hell, you don't need Rails. But those are the idiomatic layers of the stack, for a non-trivial part of the Ruby development community.
- lateefj 12y agoSorry for the confusion JVM = Java Virtual Machine, RVM = Ruby Virtual Machine, PVM = Python Virtual Machine. What I was trying to point out the layers of vitalization just to run an application. Docker is yet another layer on top of the operating system (a virtual machine running inside an operating system inside an operating system possible ect).
- regularfry 12y agoAh, ok. I can see where you're coming from, but I don't really think of Docker as being an extra layer on top. It's more a way of rearranging the OS layer itself along a certain axis. The cognitive overhead is similar to a virtualisation layer though, so that point stands.
- icebraining 12y agoDocker is used by a non-trivial part of the Ruby community? I find that hard to believe.
- regularfry 12y ago