It's funny. These days I hear Google's name mentioned in reference to subjects
I never would have imagined three or four years back. Cell phones...
Web browsers... Operating Systems... And a systems programming language???
Yes, a systems programming language... By the name of "Go", actually. It boasts garbage collection, enhanced safety and slick concurrency. The idea is that it should perform only slightly slower than C but feel more like python; simple, safe, garbage-collected and plumbing-light.
Considering it was just released to the public a few days ago and I've only been fiddling with it for a few hours now I certainly can claim no authority on the subject. I would, however, like to share some of the little hello-world-style hacks I whipped out while getting myself acquainted with Go.
Edit 12/2/2009: Note that I've launched PhatGoCode.com, a site full of Go example code.
Hello World
Here's a basic Hello World app.
package main
/* load the "fmt" package. */
import "fmt"
func main() {
/* write formatted text to console */
fmt.Println("hello world");
}
Output:
hello world
Hello World 2
Now wrapped in a function. Notice the type follows the identifier in the argument declaration, the opposite of most languages.
package main
import "fmt"
func WriteStuff(message string) {
fmt.Println(message);
}
func main() {
WriteStuff("hello world");
}
Concurrency
Concurrency is accomplished by a concept called "goroutines". The implementation is quite elegant. All you have to do is precede your function call with the "go" keyword. Quite nice!
package main
import (
"fmt";
"time";
)
func doStuff(message string) {
for i := 0; i < 10; i++ {
fmt.Println(message);
time.Sleep(100);
}
}
func main() {
/* the "go" spawns the concurrent goroutine */
go doStuff("goroutine");
/* the lack of go runs on the caller */
doStuff("main");
}
Resulting in output similar to:
goroutine goroutine main main goroutine goroutine main main goroutine goroutine main main goroutine goroutine main main goroutine goroutine main main
Concurrency 2
Here's basically the same thing, but lambda-like.
package main
import (
"fmt";
"time";
)
func main() {
/* the "go" spawns another thread, lambda style this time */
go func() {
for i := 0; i < 10; i++ {
fmt.Println("goroutine");
time.Sleep(100);
}
}(); /* parens indicate we're calling a func */
for i := 0; i < 10; i++ {
fmt.Println("main");
time.Sleep(100);
}
}
Channels
Communication between threads is accomplished via channels a la Erlang or Stackless Python. More concurrent goodness!
package main
import (
"fmt";
"time";
)
func doStuff(id int, c chan string) {
var s string;
for i := 0; i < 3; i++ {
/* receive from the channel */
s = <-c;
fmt.Printf("%d: %s\n", id, s);
}
}
func main() {
/* create a channel with string messages */
c := make(chan string);
/* spawn two goroutines that will receive messages */
go doStuff(1, c);
go doStuff(2, c);
/* send messages to the goroutines via our channel */
c <- "message 1";
c <- "message 2";
c <- "message 3";
c <- "message 4";
c <- "message 5";
c <- "message 6";
time.Sleep(1000);
}
Resulting in output similar to:
1: message 1 1: message 3 2: message 2 1: message 4 2: message 5 2: message 6
Maps
Handy key-value storage is available through maps.
/* create and initialize a map that contains floats
and is keyed on strings. */
stocks := map[string] float {
"JAVA" : 8.67,
"MSFT" : 29.62,
"GOOG" : 572.05
};
fmt.Printf("Google is %f\n", stocks["GOOG"]);
/* add a pair to a map */
stocks["ORCL"] = 22.31;
fmt.Printf("Oracle is %f\n", stocks["ORCL"]);
Conclusion
Well, that's the first few hours of hacking around. Obviously this just scratches the surface. I'm looking forward to seeing what people actually produce with Go.
It's interesting, though. We've been so wrapped up in abstracting everything away into runtimes that we may have ignored the possibility of making native/systems languages more productive. Perhaps if it catches on Go will change that.
With its ease of use, memory safety, and simplified concurrency I certainly think it has a shot of improving our lower-level lives.

At several points in my .Net development career I've had the need to make an
application I wrote scriptable. Sometimes it was to provide easy product
extension to customers or lower level information workers. Sometimes it was to
ease maintenance of very fine grained logic that has the capacity to change
frequently or unpredictably. But every time I found it to be one of the more
interesting facets of the project at hand.
Early in .Net's history this was made easy by using Visual Studio for Applications (VSA) which allowed you to host arbitrary C# or VB.Net code within the executing AppDomain. Unfortunately VSA was plagued with resource leak problems and was therefore impractical in most enterprise situations. VSA was eventually deprecated.
One of the many alternatives is to perform dynamic, on-the-fly code compilation. While certainly quite manageable it was a bit more complex and much akin to cutting down a sapling with a chainsaw.
Another option that came along later is Visual Studio Tools for Applications which brought the Visual Studio IDE to the scripter.
My favorite avenue, however, is to host a Dynamic Language Runtime (DLR) and use a language like IronPython. Not only is it disgustingly simple to implement from a plumbing point of view but Python itself seems like a natural fit due to it's simplicity. IronRuby's another wonderful choice but I'll stick to IronPython for the scope of this post.
Examples
The demonstration I'm about to show you was done using Visual Studio 2008 and IronPython 2.6 RC2. All you have to do is reference:
- IronPython.dll
- Microsoft.Scripting.dll
- Microsoft.Scripting.Core.dll
All of the following examples require the following imports:
using System; using IronPython.Hosting; using Microsoft.Scripting.Hosting;
This is very basic. It simply executes a Python print statement.
static void Main(string[] args)
{
/* bring up an IronPython runtime */
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
/* create a source tree from code */
ScriptSource source =
engine.CreateScriptSourceFromString("print 'hello from python'");
/* run the script in the IronPython runtime */
source.Execute(scope);
}
which produces:
hello from python
Scripting isn't very useful if the script can't affect the AppDomain around it. Here's an example that modifies an integer from the calling program.
static void Main(string[] args)
{
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
/* create a Python variable "i" with the value 1 */
scope.SetVariable("i", 1);
/* this script will simply add 1 to it */
ScriptSource source = engine.CreateScriptSourceFromString("i += 1");
source.Execute(scope);
/* pull the value back out of IronPython and display it */
Console.WriteLine(scope.GetVariable<int>("i").ToString());
}
producing
2
Naturally scripts would frequently operate on domain objects in the real world:
public class Employee
{
public double Salary { get; set; }
public bool Good { get; set; }
}
The following code conditionally modifies an Employee object.
static void Main(string[] args)
{
Employee employee = new Employee() { Salary = 50000, Good = true };
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
scope.SetVariable("employee", employee);
/* a more complex script this time */
ScriptSource source = engine.CreateScriptSourceFromString(
@"
def evaluate(e):
if e.Good:
e.Salary *= 1.05
evaluate(employee)
");
source.Execute(scope);
Console.WriteLine(scope.GetVariable<Employee>("employee").Salary);
}
You can also call functions in a python script:
ScriptSource source = engine.CreateScriptSourceFromString(
@"
def fun():
print 'hello from example function'
");
engine.Operations.Invoke(scope.GetVariable("fun"));
Conclusion
As you can see there really isn't much plumbing involved in hosting an IronPython runtime. In my opinion it combines both ease and power producing nearly perfect extension.

Any more it seems that virtually all the code I write on the windows platform
ends up being a windows service. It's just the nature of the kind of work I do:
the underappreciated guts that sit far beneath the software that users directly
interact with. Obviously windows services are typically written in mainstream
.Net languages like C# or VB these days but it's also easy if not easier
to do in Python.
Requirements
- A Python Interpreter - this one's pretty obvious. Can't run Python code without a Python interpreter.
- Python for Windows Extensions - this is a wonderful, easy to install project that exposes the innards of windows to Python.
- Administrative access - you must be logged in with administrative access in order to install your service.
The Code
Once your Python environment meets the requirements outlined above you're ready to write your service. All you have to do is write an extension of the ServiceFramework class that overrides the SvcDoRun and SvcStop methods. As you can guess SvcDoRun performs the core logic of your service and SvcStop shuts it down.
I've created a sample service below that simply writes some text to a file every five seconds until the service is stopped. Note that this example uses windows signal events to trigger service shutdown via the CreateEvent, WaitForSingleObject and SetEvent windows API calls. Thank you Windows Extensions!
import win32service
import win32serviceutil
import win32event
class PySvc(win32serviceutil.ServiceFramework):
# you can NET START/STOP the service by the following name
_svc_name_ = "PySvc"
# this text shows up as the service name in the Service
# Control Manager (SCM)
_svc_display_name_ = "Python Test Service"
# this text shows up as the description in the SCM
_svc_description_ = "This service writes stuff to a file"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self,args)
# create an event to listen for stop requests on
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
# core logic of the service
def SvcDoRun(self):
import servicemanager
f = open('test.dat', 'w+')
rc = None
# if the stop event hasn't been fired keep looping
while rc != win32event.WAIT_OBJECT_0:
f.write('TEST DATA\n')
f.flush()
# block for 5 seconds and listen for a stop event
rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)
f.write('SHUTTING DOWN\n')
f.close()
# called when we're being shut down
def SvcStop(self):
# tell the SCM we're shutting down
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
# fire the stop event
win32event.SetEvent(self.hWaitStop)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(PySvc)
Service Installation
Now that the service itself is written we can install it as follows:
C:\Dev\Projects\PySvc> python.exe .\PySvc.py install
Service Control
That's it! The service can now be started from the command line by
C:\Dev\Projects\PySvc> NET START PySvc
or from the Service Control Manager

One of the more popular thing's Scott Hanselman has done is maintain a tool List,
essentially a list of applications and utilities that he's found useful. There are
plenty of absolute gems in his list and rather than present my own I'd be very
comfortable just pointing you to his... But what fun would that be? None! I
also think there are a few more that I can add that are relevant to my personal
experience.
The list is rather short and there's a bit of overlap with Scott's but if all goes as planned I'll expand it over time as old useful tools come to mind or I discover new ones.
Cloud
iAWSManager - This is an iPhone app that lets you manage many of the functions of Amazon Web Services such as S3, SimpleDB and EC2.
Rackspace Cloud Servers iPhone App - Here's an iPhone app that allows you to deploy and manage virtual servers hosted on Rackspace Cloud Servers.
Elasticfox - This is a firefox plugin that facilitates full management of Amazon's EC2 within firefox.
S3Fox Organizer - This is a firefox plugin that facilitates full management of Amazon's S3 within firefox.
SimpleDBExtension - OK, here's some shameless self promotion. SimpleDBExtension is a SQL Server Reporting Services data processing extension that facilitates using Amazon's SimpleDB as a data source. It's written by yours truly and is released under the MIT License.
SDB Tool - Here's a FireFox plugin that is like the query analyzer for Amazon's SimpleDB.
Editors
KomodoEdit - This is a great, multiplatform (Linux, Windows and Mac) and free text editor that's specifically targeted at dynamic language development. It's developed by ActiveState, a company who's bread and butter is dynamic languages.
jed - This is the text-mode editor I've been using under unixes for a LONG time. It's compatible with emacs commands, has syntax highlighting and friendly pull-down menus.
System
Virtual CloneDrive - This let's you mount ISO images in Windows and is compatible with Windows 7 64-bit. The price is right too... It's free.
Sun VirtualBox - VirtualBox is an open source desktop virtualization system (similar to VMWare Workstation) developed by Sun Microsystems. It's multi-platform, feature-rich and free.
Cygwin - This is one of my all time favorites. It's essentially a mini unix-like environment that runs under windows as an application. It's not a VM, more like unix shell for windows. It boasts an X server and gives you access to all the goodies unix guys are used to like sed, awk and grep.
putty - The defacto Telnet/SSH client for windows.
PowerShell - For years windows suffered from having an infirior shell in cmd.exe. Those days are decidedly gone with PowerShell. Although it's been around a while now it seems that broad adoption has only begun to happen. It's basically an operating system shell with full .Net Framework integration. Easily the most powerful shell ever invented. If you haven't used it before be prepared to read and experiment, however. There is a learning curve.
Development
NDepend - Here is a very impressive refactoring and code management tool for .Net. My favorite feature is it's Code Query Language (CQL) which allows a programmer to answer questions about their code with a programmer-friendly SQL like syntax.
DB40 - This is an object oriented database for Java and .Net. A shockingly easy way to persist data.
Modify Headers - Firefox plugin that allows you to manually set HTTP headers.
Reflector - .Net assembly analysis tool by Red Gate with Visual Studio integration.
ComponentOne Controls - Control suites for Win Forms, WPF, ASP.Net, Silverlight, iPhone and more.
MongoHub - Native Mac GUI management tool for MongoDB.
Graphics
GIMP - GIMP is basically the open source answer to photoshop. It's quite powerful but a left-brainer like myself uses it more as a utility rather than a creative outlet. Still it solves all my image manipulation troubles across all the platforms I use.

I'm continuously encouraged by the influence dynamic languages such as Ruby and
Python have had on mainstream runtimes like the CLR and JVM. Direct ports like
JRuby, Jython to the JVM and IronRuby
and IronPython to the CLR are truly exciting. More
exciting still are languages like Boo that are built from the ground up for
mainstream runtimes.
I've finally had the chance to fool around with a language I've been dying to dig in to that was built specifically for the JVM: Groovy.
Groovy is an agile, dynamically-typed, well supported language that is perhaps most famous as the basis for Groovy on Grails, a rails-like web development framework.
Example
As I typically do I'll show you a quick twitter status example to get your feet wet. Consider the following class written in Groovy:
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.net.URL
import javax.xml.xpath.*
import javax.xml.parsers.*
class TwitterUser {
/* definition of UserName property. public accessors and mutators
are automatically created. */
def UserName
/* constructor definition */
def TwitterUser(userName) {
this.UserName = userName
}
/* public method to query twitter for the users status */
def getStatus()
{
/* pretty much just java code sans the types and semicolons */
def builder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
def document = builder.parse("http://twitter.com/users/${UserName}.xml")
def xpath = XPathFactory.newInstance().newXPath()
return xpath.evaluate( '/user/status/text', document,
XPathConstants.STRING)
}
}
This Groovy class can now be consumed by Java very easily:
public class Main {
public static void main(String[] args) {
TwitterUser status = new TwitterUser("chrisumbel");
/* see the accessor we didn't create? */
System.out.println(status.getStatus());
}
}
Conciseness
That's all fine and dandy, but it's practically Java. Consider the following ruby-like block which defines a list and iterates it:
[2.5, "yo", 5].each() {
/* "it" is the current item */
println it
}
Ranges are also supported:
(0..3).each { i ->
println i
}
Ruby/Python-ish indexing as well:
def list = [2, 4, 6, 8] println list[1..2]
which produces:
[4, 6]
and
def words = "Hack off a word" println words[0..-6]
resulting in:
Hack off a
Nice maps too:
def map = ["name" : "Chris Umbel", "age" : 30] println map["age"]
Real Life
Considering it fits right into NetBeans Groovy is an incredibly accessible language. Having a first class IDE never hurts. The fact that groovy classes can sit in the same project with Java classes removes pretty much all barriers (and excuses). The nature of the runtime and the tooling proves that Groovy is ready for prime time.

Digg it
Reddit
Delicous
Facebook










