Session 1 · Hello, Go and the basics#

Watch the class#

The big idea#

Go is a small, fast language made at Google for building servers and command-line tools. It is compiled: you write text files, the compiler checks them, and you get one native program that runs anywhere with no extras to install. It is also statically typed: every variable has a type that is fixed and checked before the program ever runs, so a whole class of typos never reach your users.

Everyday example: a recipe card. The card says exactly what each ingredient is (“200 g flour”, “2 eggs”). A cook who reads “2 flour” is stopped at once, before anything goes in the oven. That early stop is what the Go compiler does with your types.

In plain words

A Go program is a file that says package main, imports what it needs, and has a function called main. Go starts there. Variables have types, loops are all spelled for, and the compiler will not let you leave a variable or import unused.

Reach for Go when:

  • you want one small, fast program to copy to a server

  • you are writing a web API, a CLI tool or something that does many things at once

  • you like a language that is easy to read in a year’s time

From hello.go to output go run compiles your source into a native program, runs it, and throws the program away; go build keeps it. hello.go source code compiler checks types go build hello one native file run Hello, Go! printed output package main · import "fmt" · func main() { … } Every program: a package main and a func main as the starting point go run = build + run + delete · go build = build and keep
go run compiles your source into a native program, runs it, and throws the program away; go build keeps it.

Hello, Go#

hello.go: the smallest useful program, plus formatted printing.

package main

import "fmt"

func main() {
	fmt.Println("Hello, Go!")
	fmt.Print("no newline here")
	fmt.Print("\n")

	name := "Asha"
	age := 28
	fmt.Printf("Name: %s, Age: %d\n", name, age)
}
Hello, Go!
no newline here
Name: Asha, Age: 28
  • package main marks a program (not a library). func main() is where it starts.

  • fmt.Println prints and adds a newline; fmt.Print does not.

  • fmt.Printf fills in verbs: %s for a string, %d for an integer, %v for “anything”, %T for the type, %+v for a struct with field names.

Run it with go run hello.go. To keep the program as a file, use go build hello.go and run ./hello.

Variables, constants and types#

variables.go: three ways to make a variable, and what you get if you give it no value.

package main

import "fmt"

func main() {
	var city string = "Coimbatore" // full form
	var year = 2026                // type inferred
	country := "India"             // short form, only inside functions
	const pi = 3.14159             // never changes

	var count int      // zero value: 0
	var ready bool     // zero value: false
	var label string   // zero value: "" (empty)

	fmt.Println(city, year, country, pi)
	fmt.Println(count, ready, label == "")
	fmt.Printf("%T %T %T %T\n", city, year, pi, ready)

	a, b := 7, 2
	fmt.Println(a/b, a%b, float64(a)/float64(b))
}
Coimbatore 2026 India 3.14159
0 false true
string int float64 bool
3 1 3.5
  • var name type = value is the full form. Leave out the type and Go works it out.

  • name := value is the short form you will use most. It only works inside functions.

  • const values never change.

  • A variable you declare but do not set gets its zero value: 0, false, "", nil. There is no “undefined” in Go.

  • Go never converts numbers quietly. 7/2 is 3 because both are integers. Say float64(a)/float64(b) when you want 3.5.

Decisions and loops#

flow.go: if, for (the only loop), and switch.

package main

import "fmt"

func main() {
	age := 16
	if age >= 18 {
		fmt.Println("Eligible to vote")
	} else {
		fmt.Println("Not eligible yet")
	}

	// the classic three-part loop
	for i := 1; i <= 3; i++ {
		fmt.Println("lap", i)
	}

	// Go has no while: a for with only a condition
	n := 3
	for n > 0 {
		fmt.Println("countdown", n)
		n--
	}

	// switch: no break needed, first match wins
	day := 6
	switch day {
	case 6, 7:
		fmt.Println("weekend")
	default:
		fmt.Println("weekday")
	}

	// if with a short statement before the condition
	if square := age * age; square > 200 {
		fmt.Println("big square", square)
	}
}
Not eligible yet
lap 1
lap 2
lap 3
countdown 3
countdown 2
countdown 1
weekend
big square 256
  • No brackets around conditions, but braces are required.

  • for does the job of for, while and do-while. for {} loops forever until you break.

  • A switch case stops by itself; there is no fall-through, and one case can list several values.

  • if square := age * age; square > 200 { declares a variable that lives only inside that if.

Note

gofmt (run by go fmt or your editor on save) lays out your code in the one standard style. Do not argue about tabs and braces; let the tool decide.

Common mistakes#

  • Unused variable or import. x declared and not used is a compile error, not a warning. Use the value, or write _ = x while experimenting.

  • := outside a function. At the top of a file use var.

  • Expecting 7/2 to be 3.5. Integer division drops the fraction.

  • := shadowing. In x := 1 inside an inner block you create a new x; the outer one is unchanged.

  • Thinking a switch falls through like C or Java. It does not.

Hands-on exercises#

Try each one before opening the solution.

Exercise 1 · Greeter. Declare name and age with := and print Hi, I am Asha and I am 28. using Printf.

Solution
package main

import "fmt"

func main() {
	name := "Asha"
	age := 28
	fmt.Printf("Hi, I am %s and I am %d.\n", name, age)
}
Hi, I am Asha and I am 28.

Exercise 2 · FizzBuzz. For 1 to 15 print Fizz for multiples of 3, Buzz for multiples of 5, FizzBuzz for both, otherwise the number.

Solution
package main

import "fmt"

func main() {
	for i := 1; i <= 15; i++ {
		switch {
		case i%15 == 0:
			fmt.Println("FizzBuzz")
		case i%3 == 0:
			fmt.Println("Fizz")
		case i%5 == 0:
			fmt.Println("Buzz")
		default:
			fmt.Println(i)
		}
	}
}
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

A switch with no value after it is a tidy if / else if chain: the first true case wins.

Exercise 3 · Celsius table. Print a table from 0 to 40 °C in steps of 10 with the Fahrenheit value (c*9/5 + 32) to one decimal place.

Solution
package main

import "fmt"

func main() {
	for c := 0; c <= 40; c += 10 {
		f := float64(c)*9/5 + 32
		fmt.Printf("%2d C = %.1f F\n", c, f)
	}
}
 0 C = 32.0 F
10 C = 50.0 F
20 C = 68.0 F
30 C = 86.0 F
40 C = 104.0 F

Convert c to float64 first; mixing an int and a float64 in one expression will not compile.

Source files for this session

hello.go · variables.go · flow.go