Archive for the ‘ Programming ’ Category

In this post I’ll be going over how to install Sublime Text under Ubuntu and add in the plugins for GoSublime and GoToDoc which add in enhancements to Sublime for the Go programming language. First you’ll want to download Sublime itself: http://www.sublimetext.com/2

Once that’s done, open up a command line and unpack and install the files. In the below I’ll be assuming Sublime was downloaded into Downloads and I’ll be installing it under /opt/

cd Downloads
tar xvfj Sublime\ Text\ 2.0.1.tar.bz2
sudo mv "Sublime Text 2" /opt/Sublime_Text_2

Next add /opt/Sublime_Text_2 to your PATH in ~/.bashrc

echo "export PATH=\$PATH:/opt/Sublime_Text_2" >> ~/.bashrc

source ~/.bashrc

You’ll now be able to start up the software simply by running the command: sublime_text

The base Sublime install doesn’t include good support for Go. For that you’ll need a couple of external packages. To make these packages easier to install you’ll want to install something called Package Control. Package Control allows you to easily install packages from within Sublime.

Open up Sublime and press the Control key plus the ` key. This will open up the Sublime console. Copy and paste the below into it:

import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read()); print 'Please restart Sublime Text to finish installation'

Once that’s done you’ll be prompted to restart Sublime. Now that you have Package Control installed you can install GoSublime and GoToDoc. Fire up Sublime again and press Control Shift and P. This will bring up a text prompt at the top of the screen. Start typing in Package and choose the Package Control: Install Package from the drop down list.

Choosing that will prompt you for a package to install. Start typing in GoSublime and choose that.

GoSublime requires two other packages to handle syntax highlighting and other Sublime features. Press Control Shift and P again and start typing in “GoSublime: Install/Update MarGo and Gocode” and choose that.

Once that’s done we can install GoToDoc. Press Control Shift P again, start typing in Package, choose Package Control: Install Package and when prompted for the package to install start typing in GoToDoc.

For an example on how to use these plugins open up a simple “Hello World” hello.go file. Once that’s open in Sublime press and hold the Control key, then press comma and then R. That will run the code.

Most command for GoSublime use Control plus comma plus something else. You can see a list of what you can do by typing Control Shift P and start typing in GoSublime. That’ll give you a list of GoSublime functions and the hot key combinations to run them.

To use GoToDoc simply highlight a package name in the file(like fmt for example) and press Control Alt E. That’ll bring up a window in your web browser that goes to the documentation for that package. GoToDoc also has a Control Alt L function, but that conflicts with Ubuntu’s screen locking. So if you want that functionality(read more on it on the GoToDoc page) you’ll either need to remap it or remap Ubuntu’s screen locking.

In this post I’ll be going over how to setup Google’s Go in Ubuntu 12.04 64bit version. If you follow these steps you’ll have the build tools for Go installed which can compile for 386 or amd64 systems.

First you’ll need to install Subversion, Git and Mercurial which Go uses to download packages. In addition you’ll need some basic C build tools installed which will be used to compile Go itself. From the command line run the following:

sudo apt-get install mercurial git subversion gcc libc6-dev libc6-dev-i386

Next make sure you’re logged in as your normal user account. Add the below to your .bashrc file:

#This line will tell the Go installer where to place the source code before compilation
export GOROOT=$HOME/go

#With this line, you choose the architecture of your machine.
#Those with 64 bit CPUs should enter "amd64" here.
export GOARCH=amd64

#Your operating system
export GOOS=linux

#And now the location where the installer will place the finished files
#Don't forget to create this directory before installing
export GOBIN=$GOROOT/bin

#Include Go binaries in command path
export PATH=$PATH:$GOBIN

When you do the above either run “source ~/.bashrc” to load the new settings or close and re-open a new terminal window.

Now you can install Go itself:

cd ~
hg clone -u release https://code.google.com/p/go

cd go/src/
./all.bash

cd ~

This should install the basic Go build tools. Test this by putting the below code into a hello.go file:

package main

import "fmt"

func main() {
fmt.Printf("hello, world\n")
}

And then compile and run it:

go build hello.go

./hello

The above will have created a 64bit binary. But we can also setup Go to be able to compile i386 binaries:

cd ~/go/src/
GOARCH=386 ./all.bash

Once that’s done, go back and re-compile hello as an i386 binary:

cd ~
GOARCH=386 go build hello.go

Note that this binary will still run on your 64bit Ubuntu system since you have 32bit compatibility libraries installed on your system.

I’m not a heavy programmer. I started in on it back in the 80’s and made a split career of programming and building systems in the 90’s, but by the mid 2000’s I went off into Systems Engineering and focused on “programming” large scale virtual server environments. Most of my job revolves around writing code to automate the monitoring and upkeep of these systems. For this level of work interpreted languages like Perl, Python and Ruby work just fine. I also work with developers in helping them deploy their code into production and again for most of what they do the “easy” languages of Perl, Python, Ruby and PHP work very well. I’d also place Java in that lineup, for while it compiles down to bytecode the JVM is really just another interpreter and the language is a far cry from the heavyweights of C and C++ that compile down into native code. The Java, Ruby, Python, Perl and PHP kids are all eating at the same table in the cafeteria while the C and C++ guys sit with their long beards in the corner arranging their peas into organized rows before eating.

One big problem with interpreted languages is deployment. A prime example is one tool I’m dependent on for day to day work, Puppet. Puppet is a program that runs on each server and checks into a master server. It downloads scripts from the master server and runs them. This let’s a sysadmin install software, add users, create cron jobs and a lot of other things across hundreds of servers simply by changing a single config file on the master server. It’s an invaluable tool and is a good use example of software written in Ruby. It doesn’t have to run quickly, it doesn’t have to have a small footprint, it just needs to work well. Which is does, many companies use it including Google.

But it’s also a good example of what’s bad about languages like Ruby. If your OS is say, CentOS 5, it may ship with Ruby version 1.8.5. This version of Ruby causes a problem with Puppet which ends up with the program gobbling up ram. So you need to upgrade to 1.8.6 which requires adding the EPEL repository for CentOS/Red Hat. On the other end of that, the newest version of Ruby is 1.9 and soon you can expect distributions to ship with it. Only Puppet doesn’t work with that version of Ruby yet. So in order to use Puppet you need this narrow window of Ruby which may or may not be easy to install on your platform of choice.

Pretty much all of the interpreted languages suffer from this. Java can break if you’re not using the right JVM. Python and Perl are pretty safe with the core interpreter, but any software you run that uses either will depend on outside libraries. PHP is the same way, depending on php-mysql, PECL, or any number of extra library addons.

Enter Go. Go is a new language released by Google that has its foundation in C but is modernized. In use it feels a lot like the newer interpreted languages, but it compiles down to native code like C and C++ do. It comes with a rich standard library which has been the foundation for people writing new libraries for MySQL, SSH, SNMP, web frameworks and so on. The language works very well with threading, is strongly typed but doesn’t require you to be wordy with it, is garbage collected, compiles quickly, and statically links all compiled binaries. And it’s that last bit which makes the language really interesting.

Here’s your standard Hello World program:

package main

import "fmt"

func main() {

fmt.Println("Hello World")
}

Building and running it looks like this:

$ go build hello.go
$ ./hello
Hello World

I can take that compiled hello program and run it on any 64bit Linux machine. I can even compile it for 32bit platforms on my 64bit machine if I wanted to. It doesn’t depend on any outside library. No, really:

ldd hello
not a dynamic executable

But let’s get a little more complicated. Let’s write a program that queries a remote database using a 3rd party MySQL library written in Go: https://github.com/ziutek/mymysql

package main

import (
"os"
"github.com/ziutek/mymysql/mysql"
_ "github.com/ziutek/mymysql/native" // Native engine
)

func main() {
db := mysql.New("tcp", "", "192.168.177.225:3306", "testuser", "TestPasswd9", "test")

err := db.Connect()
if err != nil {
panic(err)
}

rows, _, err := db.Query("select * from a where id > %d", 20)
if err != nil {
panic(err)
}

for _, row := range rows {
for _, col := range row {
if col == nil {
// col has NULL value
} else {
// Do something with text in col (type []byte)
}
}
// You can get specific value from a row
val1 := row[1].([]byte)

// You can use it directly if conversion isn't needed
os.Stdout.Write(val1)

}
}

So here this program is using the MyMySQL library to connect to a remote database on 192.168.177.225 and run a select query. Pretty standard stuff and it builds and runs like:

$go build mysqltest.go
$./mysqltest
4042

Like I said, pretty standard stuff. Let’s look what this software depends on:

$ objdump -p mysqltest | grep NEEDED | awk '{print $2}'
libpthread.so.0
libc.so.6

I basically just need 2 standard core C libraries to run this app. I don’t even think Linux will boot without these libraries. I don’t need MySQL libraries at all to run my MySQL program. As an example I can throw this app on another machine without the MySQL C libraries installed:

# rpm -qa | grep -i mysql
# ldconfig -v | grep -i mysql
ldconfig: /etc/ld.so.conf.d/kernelcap-2.6.18-194.26.1.el5.conf:6: duplicate hwcap 0 nosegneg
ldconfig: /etc/ld.so.conf.d/kernelcap-2.6.18-238.9.1.el5.conf:6: duplicate hwcap 0 nosegneg
ldconfig: /etc/ld.so.conf.d/kernelcap-2.6.18-238.el5.conf:6: duplicate hwcap 0 nosegneg

# ./mysqltest
4042

Keep in mind that I can’t even run a C program that uses MySQL without libmysqlclient.so being installed on the system. But Go doesn’t care.

This is where I think Go really becomes interesting and wins out against other languages. Imagine deployments where all you care about is 32bit or 64bit. You don’t need libs on your target machine, you don’t need complicated deployment systems that make sure a target machine has X, Y and Z. You simply build a binary and can push it out to anything built in the last decade.

Go brings along a lot of other advantages if you’re coming from an interpreted background. Compiled code is always going to be faster than interpreted(though at the moment Go’s speed is still being worked on) and it has extremely good threading support, something which a lot of interpreted languages are pretty bad with. On the down side it does away with certain concepts in the name of simplicity and code readability(generic types and polymorphism being two examples) and while it has a pretty solid core library it still lacks the decade of tools and libs that other languages have built up.