Chris Umbel

A Little More of Google's Go

Well, I've been spending a little more time fiddling with Google's new Go programming language of late and again figured I'd share some more playing-around-code.

HTTP Operations and XML Processing

One of my favorite examples I tend to use in higher level languages is the retrieval of twitter statuses with only out-of-the-box libraries. I was surprised how simple this task ended up to be with Go which is more of a systems language. The HTTP get was a one-liner and the resultant XML can be unmarshaled right into native structs.

package main

import (
       "http";
       "fmt";
       "xml";
)

/* these structs will house the unmarshalled response.
   they should be hierarchically shaped like the XML
   but can omit irrelevant data. */
type Status struct {
     Text string
}

type User struct {
     XMLName xml.Name;
     Status Status;
}

func main()  {
     /* perform an HTTP request for the twitter status */
     response, _, _ := http.Get("http://twitter.com/users/chrisumbel.xml");

     /* initialize the structure of the XML response */
     var user = User{xml.Name{"", "user"}, Status{""}};
     /* unmarshal the XML into our structures */
     xml.Unmarshal(response.Body, &user);

     fmt.Printf("status: %s", user.Status.Text);
}

Object Orientation

Go's approach to object orientation is interesting. Essentially you just tack methods onto structs as is illustrated below.

package main

import "fmt"

/* basic data structure upon with we'll define methods */
type employee struct {
     salary float;
}

/* a method which will add a specified percent to an
   employees salary */
func (this *employee) giveRaise(pct float) {
     this.salary += this.salary * pct;
}

func main() {
     /* create an employee instance */
     var e = new(employee);
     e.salary = 100000;
     /* call our method */
     e.giveRaise(0.04);

     fmt.Printf("Employee now makes %f", e.salary);
}

Go doesn't have inheritance per se but does support contracts by way of interfaces. The following code illustrates how two different kinds of things (stock positions and automobiles) share a common operation (obtaining its value).

package main

import "fmt";

type stockPosition struct {
     ticker string;
     sharePrice float;
     count float;
}

/* method to determine the value of a stock position */
func (this stockPosition) getValue() float {
     return this.sharePrice * this.count;
}

type car struct {
     make string;
     model string;
     price float;
}

/* method to determine the value of a car */
func (this car) getValue() float {
     return this.price;
}

/* contract that defines different things that have value */
type valuable interface {
     getValue() float;
}

/* anything that satisfies the "valuable" interface is accepted */
func showValue(asset valuable) {
     fmt.Printf("Value of the asset is %f\n", asset.getValue());
}

func main() {
     var o valuable = stockPosition{ "GOOG", 577.20, 4 };
     showValue(o);
     o = car{ "BMW", "M3", 66500 };
     showValue(o);
}

Tue Nov 17 2009 20:00:00 GMT+0000 (UTC)

Follow Chris
RSS Feed
Twitter
Facebook
CodePlex
github
LinkedIn
Google