Sikau

Go · Day 1 · about 35 min

Packages, variables and types

package main, :=, zero values, Go's numbers and strings.

A language built at Google

Go was designed in September 2007 by Robert Griesemer, Rob Pike and Ken Thompson at Google and announced in November 2009. They were tired of huge C++ builds that took ages to compile and of languages that made simple things complicated. Their answer is deliberately small: few keywords, one way to format code (gofmt), fast compilation to a single binary, garbage collection, and concurrency built into the language.

Today Go runs much of the cloud: Docker, Kubernetes, Terraform and countless web services are written in it. As Donovan and Kernighan put it in *The Go Programming Language*, Go has "a cultural agenda of radical simplicity" — and Teiva Harsanyi's *100 Go Mistakes* adds the warning that simple doesn't mean easy. This course teaches both: the language, and the classic mistakes that trip up even experienced Go programmers.

Every program in this course runs in a Go simulator inside your browser. It follows Go 1.23 closely — compile errors and panics use the real toolchain's wording — but it runs a teaching subset of Go (no generics, for example). Where it differs from real Go, the lesson says so.

The shape of a Go program

Every Go file starts with a package clause. An executable program is package main, and it runs func main(). Code from other packages comes in with import, and you use a package's names through its name: fmt.Println. A name is exported (visible outside its package) only if it starts with a capital letter — that's why it's fmt.Println, never fmt.println.

package main

import "fmt"

const city = "Kathmandu"

func main() {
	var visitors int
	price := 150
	visitors = 3
	fmt.Println(city, visitors*price)
}

There are two ways to make a variable. var name type works anywhere and always starts the variable at its zero value: 0 for numbers, "" for strings, false for booleans and nil for pointers, slices, maps, channels, functions and interfaces. Go has no uninitialised memory. Inside functions you'll mostly use the short variable declaration x := value, which declares and infers the type.

The rest of this lesson

  • Numbers, strings and conversionsexplain
  • Integer mathspredict
  • Zero valuesmatch
  • Convert explicitlyfill
  • Pokhara weatherwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 10 lessons of Go