Setup, Variables and Functions

Harry · 14 Sep 2026 · 1 views
Advertisement
Advertisement

Install and run

go version           # verify the install
go run hello.go      # compile and run in one step
go build hello.go    # produce a binary

Your first program

package main

import "fmt"

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

Every executable Go program lives in package main and starts at func main(). Imports bring in packages like fmt from the standard library.

Variables

var name string = "Ada"   // explicit
var age = 36              // type inferred
count := 0                // short form (inside functions only)
const Pi = 3.14159

The := short declaration is what you will use most inside functions – it declares and infers the type in one step. Go is strict: unused variables and imports are compile errors.

Functions

func add(a int, b int) int {
    return a + b
}

// multiple return values — very common in Go
func divide(a, b float64) (float64, error) {
    if b == 0 {
        return 0, fmt.Errorf("division by zero")
    }
    return a / b, nil
}

Multiple return values are idiomatic Go, especially returning a result alongside an error – the foundation of Go's error handling.

Basic types

int, float64, string, bool, plus slice (dynamic array) and map (covered later). The zero value of a type (0, "", false) is used when you do not initialise a variable – there is no null for these.

Key points

  • Run with go run, compile with go build; programs start at main().
  • Declare variables with var or the short := form; constants with const.
  • Functions can return multiple values – commonly a result and an error.
  • Unused variables/imports are compile errors; uninitialised values get a zero value.
Share this post:

Comments (0)

Please login or register to comment.