Skip to content
Sign inGet all access
Chapter 1·8 minFree

Getting Started

Running code, packages, printing, and why unused things fail the build.

Run a file

go run compiles and runs in one step:

main.go
package main

import "fmt"

func main() {
	fmt.Println("Hello from Go")
}
Output$ go run main.goHello from Go

go build produces a binary instead:

terminal
go build -o hello .
./hello
Output$ go build -o hello .$ ./helloHello from Go

Packages

Every file starts by declaring its package. main is the one that builds a program:

main.go
package main

import "fmt"

func main() {
	fmt.Println("done")
}
Outputdone

Imports

One import on a line, or a parenthesised block for several. The path is the package’s location; you call it by the last segment, so "net/http" is used as http:

main.go
package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(len("abc"))
	fmt.Println(strings.ToUpper("ada"))
}
Output3ADA

Println and Printf

Println takes any number of values, separates them with spaces and adds a newline:

main.go
package main

import "fmt"

func main() {
	fmt.Println("a", "b", 1, true)
}
Outputa b 1 true

Printf takes a format string. It adds no newline of its own:

main.go
package main

import "fmt"

func main() {
	var name string = "ada"
	var age int = 36
	fmt.Printf("%s is %d\n", name, age)
}
Outputada is 36

Sprintf returns the string instead of printing it:

main.go
package main

import "fmt"

func main() {
	var s string = fmt.Sprintf("%s=%d", "ada", 36)
	fmt.Println(s)
}
Outputada=36

The verbs

main.go
package main

import "fmt"

type Point struct{ X, Y int }

func main() {
	var p Point = Point{3, 4}

	fmt.Printf("%v\n", p)
	fmt.Printf("%+v\n", p)
	fmt.Printf("%T\n", p)
}
Output{3 4}{X:3 Y:4}main.Point

%v when you do not care about the type, %+v whenever you are debugging a struct — {3 4} alone does not say which field is which. The ones worth memorising:

Verb Prints
%v the value, in its default form
%+v a struct with field names
%T the type
%d %s %t %f int, string, bool, float
%q a quoted string
%% a literal %

Unused is an error

An unused import does not compile:

main.go
package main

import (
	"fmt"
	"os"
)

func main() {
	fmt.Println("hi")
}
Output./main.go:5:2: "os" imported and not used

So does an unused local variable:

main.go
package main

import "fmt"

func main() {
	var count int = 10
	fmt.Println("hi")
}
Output./main.go:6:6: declared and not used: count

_ is the blank identifier. Assigning a variable to it counts as a use, so the same program compiles with the variable still there:

main.go
package main

import "fmt"

func main() {
	var count int = 10
	_ = count

	fmt.Println("hi")
}
Outputhi

Comments and gofmt

// to end of line, /* */ for a block:

main.go
package main

import "fmt"

func main() {
	// A line comment runs to the end of the line.
	fmt.Println("one")

	/*
	   A block comment spans
	   as many lines as you like.
	*/
	fmt.Println("two")
}
Outputonetwo

gofmt rewrites the file to the one canonical layout:

terminal
gofmt -d messy.go
gofmt -w messy.go
Output$ gofmt -d messy.go-func Add( a int,b int ) int {-return a+b+func Add(a int, b int) int {+ return a + b