Some notes on A Tour of Go

Posted on October 5, 2011 by Tommy McGuire
Labels: programming language
I have no particular comment about this; my feelings (and the appearance of my eyebrows) should be reasonably obvious to reasonably regular readers.
Inside a function, the := short assignment statement can be used in place of the short var declaration.
(Outside a function, every construct begins with a keyword and the := construct is not available.)
"Outside of a dog, a book is a man's best friend. Inside of a dog, it's too dark to read." -- Groucho Marx.




Oddly, according to 20, the predicate of an if statement is not an expression:
The if statement looks as it does in C or Java, except that the ( ) are gone (they are not even optional)....
Or so I assume, because in most languages, expr is equivalent to (expr); the extra parentheses do nothing, but don't hurt anything either.




What the...?!
Like for, the if statement can start with a short statement to execute before the condition.
if v := math.Pow(x, n); v < lim {
return v
}


In case I seem excessively negative, they have fixed one of C's major shortcomings: The basic types specify their sizes.
Go's basic types are bool string int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr float32 float64 complex64 complex128


The syntax for map types is...odd.
var m map[string]Vertex


Functions...
are full closures. The adder function returns a closure. Each closure is bound to its own sum variable.
For those of you misusing the term "closure", please stop. A closure is a implementation technique for lexically scoped functions, not a feature in itself.

Switch statements:
A case body breaks automatically, unless it ends with a fallthrough statement.
Cry "Havoc!" and let slip the dogs of the inverse of the "break" war! Probably is a better default, though.
today := time.LocalTime().Weekday
switch time.Saturday {
case today+0:
fmt.Println("Today.")
case today+1:
fmt.Println("Tomorrow.")
case today+2:
fmt.Println("In two days.")
default:
fmt.Println("Too far away.")
}
today is an int (2 is Wednesday); time.Saturday is the int representing Saturday (6). The switch expression and the chosen case expression should match.
switch {
case t.Hour < 12:
fmt.Println("Good morning!")
case t.Hour < 17:
fmt.Println("Good afternoon.")
default:
fmt.Println("Good evening.")
}
Switch without a condition is the same as switch true.
Unless the switch expression is missing, in which case the switch is equivalent to chained if statements.

Cooperative threads?
package main

import (
"fmt"
// "runtime"
)

func say(s string) {
for i := 0; i < 5; i++ {
// runtime.Gosched()
fmt.Println(s)
}
}

func main() {
go say("world")
say("hello")
}
yields
hello
hello
hello
hello
hello


Go language shows the influence of Plan 9, which I suspect means a definite case of not-invented-here syndrome; it also seems to have a lot of local optimizations, choices that make sense for a particular case. Perl is another major language fond of local optimizations inflated to major features. I'm still not seeing a good reason to focus on Go rather than some of the alternatives. Your mileage may vary.
active directory applied formal logic ashurbanipal authentication books c c++ comics conference continuations coq data structure digital humanities Dijkstra eclipse virgo electronics emacs goodreads haskell http java job Knuth ldap link linux lisp math naming nimrod notation OpenAM osgi parsing pony programming language protocols python quote R random REST ruby rust SAML scala scheme shell software development system administration theory tip toy problems unix vmware yeti
Member of The Internet Defense League
Site proudly generated by Hakyll.