Go Programming/Print version

From Wikibooks, open books for an open world
Jump to: navigation, search

1. Introduction


Go is an open source, compiled, garbage-collected, concurrent system programming language. It was first designed and developed at Google beginning in September 2007 by Robert Griesemer, Rob Pike, and Ken Thompson.

Go aims to provide the efficiency of a statically typed compiled language with the ease of programming of a dynamic language.

Other goals include:

  • Safety: Type-safe and memory-safe.
  • Intuitive concurrency by providing "goroutines" and channels to communicate between them.
  • Efficient garbage collection "with low enough overhead and no significant latency".[11]
  • High-speed compilation.

Installing and using Go

Visit the Go programming download page and choose the option corresponding to your platform or operating system.


2. Getting Started

Hello World

Let's start with a simple example, the venerable hello world program.

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")

As you can see there is a lot going on here. The first line:

package main

starts every go source file. It states which package this file belongs to. Think of a package as a bag of code, where you are going to stuff the internals of your programs into. The main package is special, and will we get into that in a moment.

import "fmt"

The line declares all the packages (or bags of code others have written to help us do things — also known as libraries) we are going to use. In this case, we are going to use the fmt (the format package: pronounced fa-umpt) Again, in later sections, we will delve deeper into packages.

func main(){


Here we declare a function (```func```). A function is a set of instructions that we provide to the computer to get it to do what we want it to do. There are few special functions in go,