Go Lang Book

Contents

hide

Chapter 01 Tutorial

Hello World

The basic Go Hello World:

    package main
    import "fmt"

    func main() {
        fmt.Println("Hello world")
    }

To run this code, you need the Go compiler:

Get the Go compiler from: https://golang.org/dl/
Install the compiler (it should add itself to your PATH automatically)

To run this code, you can either use go run to run the file directly or go build to build the file into an executable.

Go code is organized into packages:

Each package consists of one or more .go source files
Each package must be in a single directory
Packages are similar to modules or libraries in other languages

package main defines a standalone program, rather than a module. Within package main, func main defines the entry point into the program.

import tells the compiler which other packages your package relies on

Must import exactly the packages you need - both missing imports and unused imports will result in compiler errors