Next: , Up: Tutorial   [Index]


1.1 Hello World

C is one of the most direct influences on Go, and ‘hello, world’ illustrates a number of central ideas.

package main

import "fmt"

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

Listing 1.1: gopl.io/ch1/helloworld

The Go Toolchain: go command

go run

Go is a compiled language. The Gotoolchain’ converts a source program and the things it depends on into instructions in the native machine language of a computer. These tools are accessed through a single command called go that has a number of subcommands. The simplest of these subcommands is run, which

$ go run helloworld.go
==> Hello, world!

Go natively handles Unicode, so it can process text in all the world’s languages.

go build

If the program is more than a one-shot experiment, it’s likely that you would want to compile it once and save the compiled result for later use. That is done with go build:

$ go build helloworld.go
$ # produces an executable binary file called 'helloworld'
$ ./helloworld
==> Hello, world!