Learn X in Y minutes where X = Go
learnxinyminutes.com
learnxinyminutes.com
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`.
Well, pair is an anti-pattern anyway, but what exactly would you add to "Pair"? "File not found"? :)
Perhaps a var named "brillant"
So cons is an "anti-pattern" as well, then?
Your comment is the most original dis of Lisp I've ever read. Bravo!
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.
Correct. I never thought it was and I never thought that you thought it was.
> I am referring to "Pair" as a class/interface in an OO language.
OK. I didn't get that from the context.
A truly generic Pair in go would be Pair{interface {}, interface{}}, and that would be a code smell. (Only a smell though, since it's possibly part of some larger construct that is basically getting around Go's compile-time type system and replacing it with a run-time check.)
rationals.Pair is still a poor name, it's a pair of what and for what purpose? Assuming you're referring to rational numbers, ratio struct {p, q int} is better.
do_things_with_fruits(apple_count_t apples, orange_count_t oranges);
instead of do_things_with_fruits(int apples, int oranges);
Which saves my ass when I wind up writing do_things_with_fruit(basket.oranges, basket.apples);
Note that at that point, I would say a generic pair type (not allowed in C but borrowing C++ syntax) pair<apple_count_t, orange_count_t>
is really no less informative than something like fruit_basket_t
and if it's easily defined (tuples + inference?) I see no reason to avoid it.I like the single-element structs. I've been finding myself doing that more and more in Java and I quite enjoy it. First epiphany was replacing bool with enums.
I really hope to be able to write Go professionally at some point, it is a really nice language to write in.
Thanks.
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).(Go's multiple return values seem to just be simulating a single use case of proper tuples.)
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?
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.
> 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-resultsWhen 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.
That said, I've already professed my love for the Go language, which I consider the spiritual successor of the C programming language.
Fortunately you can still have fun on the JVM, even though I prefer to avoid it these days given its current custodians.
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"])
} func main() {
var m1 map[string]int
m1["a"] = 1
fmt.Printf("%d\n", m1["a"])
}
http://play.golang.org/p/JJQ7dSZLeAwhich yields
panic: runtime error: assignment to entry in nil map type pair struct {
x, y int
}
func main() {
var p pair
fmt.Printf("%d\n", p.x)
fmt.Printf("%d\n", p.y)
}
That's just wrong, that declaration and initialization behaves differently depending on the type. m1 := map[string]int{}
m1["a"] = 1It would be my choice because unlike a lot of other pet ideas which would radically change Go into a new language, I'm pretty sure this would have hardly any effect... excepting of course to remove the Billion Dollar Mistake from the next up-and-coming language.
Just try to pass a 'map' or a 'struct' to an other function. The 'map' behaves like passed by reference and the 'struct' behaves like passed by value. If you want to pass a 'struct' by reference than you have to use a pointer to a 'struct'.
Seriously? What kind of ad hoc programming language design is this?
http://golang.org/doc/effective_go.html?ModPagespeed=noscrip...
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.
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.
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.
EDIT: Also "// x == 1 here." should be "// x == 2 here.", those are 3 iterations.
Idiomatic Go code almost never uses multi-line comments, see: 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:
And the usual amazon pricing: new $26, used $35.
fmt.Println("it's a", i)
Should be something like:
fmt.Println("it's a %T", i)
See http://golang.org/pkg/fmt/ for more options.
[update]
Forgot they allowed to send pull requests; done :)
I figured they ment to do the latter, since the next print function outputs: "it's a string".
// If statements require brace brackets, and do not require parens.
Even if it is single 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.
This way you can't go wrong adding another statement inside the if.