fimmtudagur, 26. ágúst 2010

Inheritance in Go

So. You have probably read that Go has no class construct in the language.

What about our grand object oriented design then? How do we implement it?

We can express a hierarchical relationship through composition.

This first code example shows simple inheritance. I know the code is trivial and could be written in a more clever way but that is not the point. It is there to demonstrate subclassing or if you like going from the abstract to the more specific.

Later we will talk about interfaces and see how we can use them to obtain polymorphic behavior.





package main

import "fmt"

// The base thing
type Person struct {
age int
name string
}
func (p *Person) whoAmI() {
fmt.Println("My name is", p.name, "and I am", p.age, "years old")
}

// Derived thing
type Employee struct {
Person // anonymous field (like subclassing)
company string
title string
}
// Override method
func (e *Employee) whoAmI() {
e.Person.whoAmI() // call base
fmt.Println("And I work for", e.company, "as a", e.title)
}
func (e *Employee) work() {
fmt.Println("I'm working")
}

//
type Programmer struct {
Employee
language string
}
func (programmer *Programmer) work() {
fmt.Println("I happen to program in", programmer.language)
}

//
type Manager struct {
Employee
boss bool
}
func (manager *Manager) work() {
fmt.Println("I manage")
if manager.boss {
fmt.Println("and I'm the boss")
}
}

func main() {
donald := &Person{48, "Donald Duck"}
donald.whoAmI()
fmt.Println("------------------------------------")
john := &Employee{Person{23, "John Doe"}, "KoolAid Inc", "Driver"}
john.whoAmI()
john.work()
fmt.Println("------------------------------------")
alice := &Programmer{Employee{Person{30, "Alice in Wonderland"}, "Rabbit Hole Inc", "Senior Developer"}, "Go"}
alice.whoAmI()
alice.work()
fmt.Println("------------------------------------")
bill := &Manager{Employee{Person{55, "William Bar"}, "FooBar Inc", "CxO"}, true}
bill.whoAmI()
bill.work()
}


And the output is

My name is Donald Duck and I am 48 years old
------------------------------------
My name is John Doe and I am 23 years old
And I work for KoolAid Inc as a Driver
I'm working
------------------------------------
My name is Alice in Wonderland and I am 30 years old
And I work for Rabbit Hole Inc as a Senior Developer
I happen to program in Go
------------------------------------
My name is William Bar and I am 55 years old
And I work for FooBar Inc as a CxO
I manage
and I'm the boss


sunnudagur, 22. ágúst 2010

Emacs


Ah, the joy of Emacs.
Nothing beats it. A cup of coffee and emacs is the best way to start the day.
Unless you run into problems. Like I'm running into now. Running the Gtk-snapshot version on Ubuntu 10.04.
Using the beautiful Inconsolata font.
Sometimes when I switch to another application and come back to Emacs it refuses to obey my commands (freezes). The menu is responsive but the cursor and key movements in the editor are at a halt. I have to press Ctrl-tab Alt-tab or some random keys while the menu is active to have it talk to me again.

Anyways. This is the snapshot version so I guess it will be fixed in the sharp one.

Until then.
Happy coding.

The Go Programming Language

My first programming language was Fortran 77. It was 1983 and the computer was a VAX/780 running the VMS operating system.

Initially, I thought, that programming was supposed to be like that. Strict formatting rules and integer variables beginning with i and k or even l and m and reals with x and y. School is supposed to be strict. Right? Wrong.

I came across Pascal on the VAX and Turbo Pascal on my PC which made me realize that programming could be relatively easy and fun.

Then, in 1987, a friend of mine told me to take a look at this C programming language. All the cool kids were using it. There was the Microsoft C compiler for the PC, and of course the brilliant Turbo C from Borland. That was it. I was completely blown away. The language was so small and yet, so powerful. What a blast.

Well. That was then. This is now. In the days of virtual machines running java bytecode or .NET it is hard for a grown up programmer to get excited. Yet, that is exactly how I feel now. Excited. After spending some evenings and weekends with the new offspring of Google masterminds, Rob Pike and Ken Thompson, I feel young again.

So. It is my intent to publish some essays on this new language called Go. It has some interesting features but can be difficult if you have been programming in an object oriented language and your brain is tuned for class hierarchies.

So. Stay tuned.

Here is a link to the oficial Go site.