I have just seen the introductory video
This I what I liked ( from what I understood )
wrong
if x > 0
return 0;
or
if x > 0 return 0;
right
if x > 0 {
return 0;
}
or
if x > 0 { return 0; }
always!!
Visible outside the package:
func Print(v ...) {
}
Not visible outside the package
func print( v ... ) {
}
Yes, it may be an small improvement in the code syntax too, yet, important and a easy way to create public methods/attributes etc. and finish with a lot of discussions.
- Interface you can plugin "anything" that accomplish the interface ( like in duck type if it says quack is a duck ) and yet have compile time checking for safety.
So the interface declare the quack method
type Quack interface {
DoQuack()
}
And then we can may create a function using that type:
func callIt( duck Quack ) {
duck.DoQuack()
}
To use it we only need "something" that respond to the message, for instance, a Duck
and a Person
they perform the function DoQuack
diferently
type Duck struct {
count int
}
func( d* Duck ) DoQuack() {
d.count++
for i:= 0; i < d.count ; i++ {
fmt.Print("quack!" )
}
fmt.Print("\n")
}
type Person struct {}
func ( o* Person ) DoQuack() {
fmt.Printf("Ehem, I'll try it... quack?\n")
}
Finally the check if the "struct" complies with the interface is performed by the compiler when the type is used:
func main() {
var quack Quack
quack = &Duck{}
callIt( quack )
callIt( quack )
callIt( quack )
quack = &Person{}
callIt( quack )
}
prints:
quack!
quack!quack!
quack!quack!quack!
Ehem, I'll try it... quack?
Something I didn't quite like is the syntax ... :-S I don't know how to explain it, but at this point ( 2009 ) programming languages should just flow. Go syntax, makes me stop and think twice what each line is. Probably that's just matter of getting use to it.
There are plenty of features, like the channels and the goroutines ( similar to threads ) that look pretty cool, but I think It would take me some more time to grasp them.
Finally from a sample from the install page:
http://golang.org/doc/install.html
I can see that: real programmers use cat!!!
$ cat >hello.go <<EOF
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
EOF
$ 6g hello.go
$ 6l hello.6
$ ./6.out
hello, world
Nice