11 ms·
Learn X in Y minutes where X = Go
- pjungwir 13y agoWow, I'm not really interested in learning Go, but I was drawn in and made it all the way to learnInterfaces. Congrats! One question: why have named return values (in learnMemory) if you return something different? If you didn't have a return statement at all, would the function return the final value of p and q?
- jdiez17 13y ago>why have named return values (in learnMemory) if you return something different? Probably to avoid declaring the function as `func learnMemory() (int, int)`. >If you didn't have a return statement at all, would the function return the final value of p and q? No, you have to do that explicitly. You can do `return`, though, which will return the variables whose names match the definition.
- arnehormann 13y agoTo describe what you are returning. An int tells you a lot less than status404s int.
- stock_toaster 13y ago> One question: why have named return values (in learnMemory) if you return something different? If you didn't have a return statement at all, would the function return the final value of p and q? Kinda. You still have a return statement, but you may omit the arguments. See: http://golang.org/doc/effective_go.html#named-results http://golang.org/doc/effective_go.html#named-results
- pothibo 13y agoBecause the variable are already declared and ready to be used. Difference can be seen here → http://play.golang.org/p/IVPiw_2Uph http://play.golang.org/p/IVPiw_2Uph
- thenonameguy 13y agoMy hungarian translation of it: http://learnxinyminutes.com/docs/hu-hu/go/ http://learnxinyminutes.com/docs/hu-hu/go/
- NKCSS 13y agoI think one of the string format examples missing the type formatter: fmt.Println("it's a", i) Should be something like: fmt.Println("it's a %T", i) See http://golang.org/pkg/fmt/ http://golang.org/pkg/fmt/ for more options. [update] Forgot they allowed to send pull requests; done :)
- ivanvanderbyl 13y agoI just tried it, both seem correct as fmt.PrintLn takes multiple inputs
- enneff 13y agoPrintln does not take format strings. You're thinking of Printf.
- NKCSS 13y agoYup, forgot to edit that :-/
- JulianMorrison 13y agoAre you mixing up Println and Printf?
- mseepgood 13y agoNo, it's correct. It's meant to print "it's a 84".
- NKCSS 13y agoWould't you expect either "it's 84" or "it's a(n) int|decimal|other_type"? I figured they ment to do the latter, since the next print function outputs: "it's a string".
- mseepgood 13y agoSince c is a chan int it can only be an int, no other type. You could directly write fmt.Println("it's an int").
- kunil 13y ago// If statements require brace brackets, and do not require parens. Even if it is single line?
- nanofortnight 13y agoYes
- kilburn 13y agoYes, but you shouldn't ever see single-line ifs in go, because gofmt forces all of them to be multi-line. This is one of the strong points of go (whether you like it or hate it): gofmt imposes the same formatting rules for everyone using the language. It's not a choice for you (or your project) to make. Period.
- omaranto 13y agoIt's easy enough to never run gofmt, so there is still a choice. :) In fact, not running gofmt requires less effort than running it.
- melvinmt 13y agoNot if you install the (excellent) GoSublime plugin in Sublime, it will run gofmt on every save.
- omaranto 13y agoLearning to use some newfangled editor and figuring out how to install plugins for it when the editor you've always used works just fine sounds like much more work than running gofmt. :)
- coldtea 13y agoYes. After all that's the best style in C and other brace languages too. This way you can't go wrong adding another statement inside the if.
- 13y ago
- enneff 13y agoI really love this format. Such a nice document.
- buro9 13y agoThis is the way I like to learn a language. Instantly, and progressively, by starting at the top and working through a single document that is both a reference and a guide. Beautiful work. Edit: One change I would make is to change p := pair{3, 4} into p := pair{x: 3, y: 4} which helps your declarations keep working in the future when you extend or modify the definition of `pair`.
- mseebach 13y ago> when you extend or modify the definition of `pair`. Well, pair is an anti-pattern anyway, but what exactly would you add to "Pair"? "File not found"? :) http://thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx http://thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx
- buro9 13y agoToo true. Perhaps a var named "brillant"
- acuozzo 13y ago> Well, pair is an anti-pattern anyway So cons is an "anti-pattern" as well, then? Your comment is the most original dis of Lisp I've ever read. Bravo!
- mseebach 13y agoGo isn't a Lisp, last I checked. I am referring to "Pair" as a class/interface in an OO language. If you're using Pair to construct a linked list in a OO language, I salute you, but that's even more of an anti-pattern. Pair is an OO anti-pattern because it's an arbitrary grouping of two things in a generic container, and you shouldn't do that in OO - classes are cheap. You should make a class that represents what the collection of those two things actually are. Typical field names in generic Pair objects are 'left' and 'right' or 'first' and 'second' which tells you nothing about the values you stick into them (except the types, if you're using a strongly typed language). If, as suggested in the example, the pair holds a 'x' and a 'y', a better choice could be to call it "Point" or even "Point2D". If it's a key/value pair from a map data structure it should be "Entry" with fields "key" and "value" - etc.
- dan00 13y agoIs the documentation of go regarding the 'map' data type and the usage of the function 'make' out of date? Because I can't see any difference in: package main import ( "fmt" ) func main() { m1 := map[string]int{} m1["a"] = 1 fmt.Printf("%d\n", m1["a"]) m2 := make(map[string]int) m2["b"] = 2 fmt.Printf("%d\n", m2["b"]) }
- pothibo 13y agoI think the 2 are generally equals. make is used when you want to initialize a map, slice, channel with a given size. http://golang.org/ref/spec#Making_slices_maps_and_channels http://golang.org/ref/spec#Making_slices_maps_and_channels
- mseepgood 13y agoBoth are equivalent and none is out of date. The first is just the short-hand literal syntax. Same with &Foo{} and new(Foo).
- jerf 13y agoCompare with: func main() { var m1 map[string]int m1["a"] = 1 fmt.Printf("%d\n", m1["a"]) } http://play.golang.org/p/JJQ7dSZLeA http://play.golang.org/p/JJQ7dSZLeA which yields panic: runtime error: assignment to entry in nil map
- 13y ago
- cliveowen 13y agoI'm tired of reading that this or that language/framework is "easy-to-understand" and "fun" to use. That's a subjective statement and mostly not true. Writing code is a means to an end, you want the output of a program not a bunch of lines, so I don't see how it could be fun (which is repeated over and over again in the official documentation and various slides and talks). And the concept of easy-to-understand is determined by the level of experience of an individual plus a whole lot of factors that are beyond us, not least of which is the sheer brain power.
- enneff 13y agoWhat we mean: All of us on the team have a lot of fun writing Go. We often show each other little pieces of code that delight us. This seems to happen more when we work in Go than in the other languages we use (or have used), and so we attribute the fun-ness to Go. Pretty straightforward. I'm sorry you're so tired of it.
- cliveowen 13y agoWow, I didn't expect to get a response from an actual Googler, working on Go no less. Anyhow, I also remember when people were saying that Java was a fun language to work with, but I still have to find a single engineer who doesn't think of it as slightly more than a pain in the ass. It's all a matter of right-sizing expectations; using and objective language keeps you from being called out should those expectations fail to be matched. That said, I've already professed my love for the Go language, which I consider the spiritual successor of the C programming language.
- aeden 13y agoJava was fun to play with back in the early days. It was fast, had a VM, garbage collection and was OO. Sadly the fun was gradually removed over the years as a result of design-by-committee and a desire to try to solve some of its core issues. Fortunately you can still have fun on the JVM, even though I prefer to avoid it these days given its current custodians.
- 13y ago
- ovi256 13y agoThanks! Inspired by you, just wrote the Romanian translation for Python. Maybe I'll do Go next :) Seriously thinking about writing the Django equivalent of Michael Hartl's tutorial for Rails. Intimidating as it's a lot of work, over 100k words. Any people would like this? I really could not find a Django/Python equivalent.
- jamesjporter 13y agoThat would be awesome, I've found the existing Django tutorial rather wanting.
- deleted 13y ago[deleted]
- defly 13y agoNeed for Clojure.
- Posibyte 13y agoI believe they have one: http://learnxinyminutes.com/docs/clojure/ http://learnxinyminutes.com/docs/clojure/
- NoodleIncident 13y agoThat was one of the first languages covered by this site.
- cliveowen 13y agoThis is very well useful and very well done, may I suggest to keep things consistent and actually use the syntax /* */ for multi-line comments? EDIT: Also "// x == 1 here." should be "// x == 2 here.", those are 3 iterations.
- mseepgood 13y ago> I suggest to keep things consistent and actually use the syntax /* */ for multi-line comments Idiomatic Go code almost never uses multi-line comments, see: http://golang.org/src/pkg/net/http/request.go http://golang.org/src/pkg/net/http/request.go > Also "// x == 1 here." should be "// x == 2 here." No, it's correct. := declares a new variable. The newly declared, inner x shadows the outer x only within the block scope of the for loop: http://play.golang.org/p/cP2dWyg0qw http://play.golang.org/p/cP2dWyg0qw
- cliveowen 13y agoYeah, right, I didn't see the external variable. I might as well go ahead and delete the comment :P
- reyan 13y agoThis is great. I suggest adding fallthrough statement in one of the switch cases.
- deleted 13y ago[deleted]
- gutsy 13y agoMULTIPLE return values?! My mind is BLOWN. That's awesome. My work is all in Java and there are so many times on my last project where that would have been helpful (I know I can do it by creating a map or an array or something, but that's stupid and extra code that I shouldn't need to write). I really hope to be able to write Go professionally at some point, it is a really nice language to write in.
- dllthomas 13y agoIf that blows your mind, check out Haskell - you can dispatch on type of return value.
- samatman 13y agoThis is the first one-liner I've read that actually explains something useful I can do with higher-order type inference. Preventing errors at compile time doesn't count; doing something is run-time by definition. Thanks.
- th0114nd 13y agoCare to elaborate?
- dllthomas 13y agoAs an easy to follow (though questionably idiomatic) example, consider the typeclass Default: class Default a where def :: a and an unwise pair of instances: instance Default Int where def = 7 instance Default String where def = "foo" This means I can say: 3 + def + length ("c" ++ def) and get back 14. Note that which "def" depends on the type expected where it is used. --- It's not limited to values, either. Consider the "return" function in the Monad typeclass: return :: Monad m => a -> m a which is a function that takes something and returns that something wrapped in a monad. Which monad? Whatever is expected at the call site. [1, 2, 3] ++ return 4 gives us [1, 2, 3, 4] because list is a monad and return for lists gives a single element list, whereas putStrLn "foo" >> return 4 gives us an IO action that, when executed, prints "foo" and yeilds a 4. --- A super complex example is variadic functions like printf, with the type printf :: PrintfType r => String -> r PrintfType can be a String or IO (), giving you something like C's sprintf or printf based on the call site (which is itself cool), but it can also be a function that takes an instance of PrintfArg and gives back some new PrintfType (in an interesting intersection with currying).
- chollida1 13y agoThis looks very similar to the "easter egg" that the F# team put into visual studio. When you create a new F# project you can select F# Tutorial and it generates a working project with a source code file that is an introduction to the language and its features. I wish more IDEs did this.
- prezjordan 13y agoIt's killing me that no one has made an APL version yet!
- munchor 13y agoReally great way of learning the overall concepts of Go, but that code should have been gofmt'd!
- mrcwinn 13y agoHm, would it not be where x := Go?
- emehrkay 13y agoWhere exactly does the struct pair implement the Stringer interface? Is it when the String method was added to it? What if you have interfaces with similar names?
- thisishugo 13y ago>Is it when the String method was added to it? Yes, that is how you satisfy an interface in Go. >What if you have interfaces with similar names? The problem of interfaces with similar names is actually no different than the problems of, for example, packages or functions having similar names. It's down to you to avoid naming things in a confusing manner.
- ominous_prime 13y agoThere is no "implements" in go. A type either provides the methods to satisfy an interface, or it doesn't. The name of the interface doesn't matter, and it can satisfy an many interfaces as it wants.
- antimatter 13y agoLoved this. Can someone point to a relatively simple open source project written in Go that I can dig into to learn further?
- cwills 13y agoDoes it bother anyone else that Go supports the GOTO statement? I would've thought GOTO an obvious construct to leave out when creating a new language, to protect programmers from themselves? (See Dijkstra on 'GOTO statement considered harmful')
- Tloewald 13y agoI thought the comment "x == 1 here" could use a little more explanation (I figured it out, though, so maybe not.) I also found the function signatures confusing, especially the function with a local variable q that is nothing to do with its returned value q -- seems like a bit of a wart in Go. (Being able to assign return values vs. using return, as in Pascal, seems like an obvious reason to declare return values this way. Perhaps this feature exists but is not explained.)
- cldr 13y ago> It’s not the latest trend in computer science Uh, I think it is. I can't go three articles without seeing Go mentioned. I'm going to get downmodded to gray, but either this tutorial is missing something or Go is way over-hyped. I saw nothing special in the language at all, I have no idea what all the obsession is about lately.
- gtani 13y agoI've been reading Chisnall's Phrasebook and Summerfield's Programming in Go books. Both are well done as far as descriptions of language features and standard lib go (covering 1.0), but are a little short on extended code examples. And the usual amazon pricing: new $26, used $35.