go labs

A Rubyist Learning Go – A basic Go program

Starting July 1st, I am going to have the opportunity to step outside by development comfort zone and begin working on the Cloud Foundry logging system team. Why is it outside my comfort zone? The biggest reason is that is it being written in Go. The second is that it is going to have performance requirements that I’m not used to being worried about. As a Web/Ruby/Rails/Javascript developer, I’ve been using a different skill set and I’m excited to get the opportunity to step outside of my comfort zone and try some new things.

I’ve been ramping up on Go in my spare time and as I learn more, I am going to share my experiences and some tips here. Let’s start off with how to write a simply Go program that runs from the command line and take arguments.

First you need to install Go. The Go team has done a great job outlining the steps so check out this page for details. Part of instructions will be writing a basic Go program to make sure everything works.

package main

import "fmt"

func main() {
    fmt.Printf("hello, worldn")
}

Line 1 of the program creates a package called “main”. You use packages in Go in similar ways you’d use modules in Ruby except everything must live in a package. All Go programs need a “main” package and it tells Go where the program starts. The official entry point of a Go program is the “main” function:

func main() {

}

See the Go language specification on Program Execution for more details.

The last part of that test program is the import statement:

import "fmt"

Think of them like a Ruby “require” statement that has a little more functionality. This specific import statement brings the “fmt” package into scope and allows you to print things to the screen.

That’s all for the initial post in this series. Please check back often for new posts. If you have any questions, please leave them in a comment and I’ll do my best to answer them. Also, if you have anything you’d like to see in this column, please let me know.