GGistDev

Hello, World in Go

A minimal tour to get started quickly.

Setup

  • Install Go from https://golang.org/dl. The Go toolchain includes the compiler, standard library, and the go command.
  • Initialize a module in a new folder (enables dependencies and reproducible builds). A module is the unit of versioning and distribution in Go:
    • go mod init example.com/hello
    • go env GOMOD shows the active go.mod

First program

Every executable program starts in package main with a func main(). Imports use full module or standard library paths. The compiler and linker produce a static binary by default.

package main
import "fmt"

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

Run it: go run . or build: go build then ./hello.

  • go run . compiles and runs the package in one step (handy during development)
  • go build produces a native binary (name comes from folder by default), which you can distribute or run later

Print utilities

The fmt package provides formatted I/O. Print writes without a newline, Println appends a newline and spaces between arguments, and Printf formats according to verbs (e.g., %d, %s).

fmt.Print("no newline")
fmt.Println("with newline")
fmt.Printf("%s %d\n", "answer", 42)
  • Printf uses format verbs (like C’s printf); remember to include \n

Project layout tip

  • One module per repo is typical; create subpackages with folders and their own package name to factor code by dependency boundaries
  • Use go list all to see packages; go doc fmt.Println to read docs; go help shows built-in help

Formatting and static checks

  • go fmt ./... formats code; most editors run it on save. Go style is enforced by tooling—avoid manual bikeshedding.
  • go vet ./... catches suspicious constructs (e.g., printf format mismatches, copylocks).
  • golangci-lint adds extra linters (optional) for deeper checks (ineffassign, staticcheck, etc.).

Functions

func add(a, b int) int { return a + b }
  • Types follow names; multiple params of the same type can share the type (a, b int).
  • Go does not support default parameters or overloading; prefer multiple small functions or option structs.

Next steps

  • Explore if, for, functions, errors
  • Learn modules and dependencies with go get and semantic import versions

Summary

  • package main + func main() is the entry point
  • Use fmt for printing; go run to execute quickly; go build creates a binary
  • go fmt and go vet keep code consistent and safe