Difference between revisions of "Go on RPi"

From eLinux.org
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 7: Line 7:
 
* [http://dave.cheney.net/2012/09/25/installing-go-on-the-raspberry-pi Dave Cheney has provided instructions] on building Go on Raspbian from the very latest source code. He has also blogged about [http://dave.cheney.net/2012/12/31/testing-go-on-the-raspberry-pi-running-freebsd Go on FreeBSD] instead.
 
* [http://dave.cheney.net/2012/09/25/installing-go-on-the-raspberry-pi Dave Cheney has provided instructions] on building Go on Raspbian from the very latest source code. He has also blogged about [http://dave.cheney.net/2012/12/31/testing-go-on-the-raspberry-pi-running-freebsd Go on FreeBSD] instead.
  
* [http://www.greenhughes.com/content/how-get-go-going-raspberry-pi Liam Green-Hughes has provided instructions] for building the current released version instead of the very tip. This is likely to be more stable in principle, as long as the ARM support is sufficiently complete.
+
* [http://www.greenhughes.com/content/how-get-go-going-raspberry-pi Liam Green-Hughes has provided instructions] for building the current released version, instead of the very tip. This is likely to be more stable in principle, as long as the ARM support is sufficiently complete.
 +
 
 +
It is also worth including Git and Mercurial version control systems because these are often used by Go tools.
 +
 
 +
    sudo apt-get install git mercurial
  
 
==Examples==
 
==Examples==

Latest revision as of 14:29, 1 April 2013

Go is potentially an exciting language to use on the Pi. Its compiler runs very fast and the programs it produces will be nearly as fast as C/C++. It's an easy language to learn for anyone used to C, C++, Java etc. It has a clean new standard API that makes it easier than many of the alternatives for beginners to write web applications, for example. But more importantly though, its concurrency model is very well suited to writing controller programs, so Go should be a good choice for those using the GPIO features.

Installation

For those using Raspbian on their Pi, building Go from source is the current route. This is quite easy but a bit time-consuming (allow about an hour). Here are two alternatives:

It is also worth including Git and Mercurial version control systems because these are often used by Go tools.

   sudo apt-get install git mercurial

Examples

Hello World

Having installed Go, create an empty folder and `cd` into it. Make sure the Go compiler is on your path and set GOPATH to where you are

   $ export GOPATH=$PWD

Now open an editor (nano hello.go) and insert something to your liking such as:

   package main
   
   import "fmt"
   
   func main() {
       fmt.Println("Hello, 世界")
   }

Save, then compile and run it.

   $ go build hello.go
   $ ./hello
   Hello, 世界

Writing a Web Application

External links