Skip to main content
    Courses/Go/Introduction to Go

    Lesson 1 • Beginner

    Introduction to Go 🐹

    By the end of this lesson you'll understand what makes Go special, you'll know what every line of a Go program does, and you'll have written and run your own first program that prints to the screen.

    What You'll Learn in This Lesson

    • What Go is and why it's compiled, fast, and simple
    • Why concurrency is built into the language itself
    • What package main does and why it's required
    • How import "fmt" pulls in code for printing
    • Why func main() is where every program starts
    • How to print output with fmt.Println and run code with go run

    1️⃣ What is Go?

    Go (also called Golang) was created at Google in 2009 to fix everyday frustrations: slow compile times, messy dependencies, and how hard it was to write programs that do many things at once. It stands on four ideas you'll feel in every project:

    • Compiled — Go translates your code straight into a single native binary (one runnable file) before it runs. There's no separate runtime to install on the machine that runs it.
    • Fast — that binary starts in milliseconds and runs close to C speeds, while the compiler itself is famously quick.
    • Simple — the whole language has only 25 keywords. There's usually one obvious way to do something, so code stays easy to read.
    • Built-in concurrency — doing many tasks at once (a web server handling thousands of users, for example) is part of the language itself via goroutines, not bolted on later.

    That combination is why Go powers Docker, Kubernetes, and a huge slice of modern cloud infrastructure. In this lesson you'll start where every Go programmer starts: a tiny program that prints text. Read the worked example below first — every line is explained — then run it yourself.

    Worked example: your first Go program
    // Every Go file starts by declaring which package it belongs to.
    // "main" is special: it tells Go this program can be run directly.
    package main
    
    // import brings in code other people wrote. "fmt" (say "fumpt")
    // is the standard library's package for formatting and printing.
    import "fmt"
    
    // func main() is the entry point. When you run the program,
    // Go calls this function first. The { must be on THIS line.
    func main() {
        // Println = "print line". It writes its text, then a newline.
        fmt.Println("Hello, World!")          // -> Hello, World!
        fmt.Println("My first Go program!")   // -> My first Go program!
    }
    Output
    Hello, World!
    My first Go program!
    This is real code — run it for free atthe Go Playgroundor in your own editor.

    2️⃣ The Four Parts of Every Go Program

    Look back at that example. Every Go program you ever write has the same four building blocks, in this order:

    • package main — declares which package this file belongs to. The name main is special: it marks a program you can run (as opposed to a library other code imports).
    • import "fmt" — pulls in the fmt package (say "fumpt"), Go's standard toolkit for formatting and printing text.
    • func main() { — defines the entry point. When you run the program, Go calls main first. The opening { must sit on the same line as func main().
    • fmt.Println(...) — the actual work. Println means "print line": it writes its values and then moves to a new line.

    This next example shows the small but important differences between Println (adds a newline and spaces) and Print (adds neither). Read the comments, predict the output, then check.

    Worked example: Println vs Print
    package main
    
    import "fmt"
    
    func main() {
        // Println can take several values; it puts a SPACE between them.
        fmt.Println("Go", "is", "fast")        // -> Go is fast
    
        // Each Println call starts a brand-new line.
        fmt.Println("Line one")                // -> Line one
        fmt.Println("Line two")                // -> Line two
    
        // Print (no "ln") does NOT add a newline — notice both words
        // land on the same line below, joined with no space.
        fmt.Print("No")
        fmt.Print("Newline")                   // -> NoNewline
        fmt.Println()                          // an empty Println ends the line
    }
    Output
    Go is fast
    Line one
    Line two
    NoNewline
    This is real code — run it for free atthe Go Playgroundor in your own editor.

    Your turn. The program below works once you fill in the two blanks marked ___. Follow the 👉 hints, then run it and compare with the expected output.

    🎯 YOUR TURN: print two lines of your own
    package main
    
    import "fmt"
    
    func main() {
        // 🎯 YOUR TURN — replace each ___ then run it.
    
        // 1) Print a greeting with your own name in it
        fmt.Println(___)   // 👉 put text in "double quotes", e.g. "Hi, I'm Sam"
    
        // 2) Print a second line about why you're learning Go
        fmt.Println(___)   // 👉 more text in "double quotes"
    
        // ✅ Expected output (example):
        //    Hi, I'm Sam
        //    I'm learning Go to build fast tools
    }
    Output
    Hi, I'm Sam
    I'm learning Go to build fast tools
    Fill in the ___ blanks, then run it for free at the Go Playground and check your output matches.

    3️⃣ Running Your Code with go run

    To run a Go file on your own machine, you save it (for example as main.go) and use the go run command in your terminal. It compiles and runs the program in one step — perfect while you're learning and experimenting.

    Run a Go file from the terminal
    # Save your code in a file called main.go, then run it:
    go run main.go
    
    # That single command compiles AND runs the program.
    # To produce a standalone binary you can ship, build it instead:
    go build -o hello main.go
    ./hello
    
    # Check which version of Go you have installed:
    go version
    Output
    Hello, World!
    My first Go program!
    These are terminal commands. Run them in your own shell after installing Go from go.dev, or skip the install entirely and use the Go Playground.

    One more guided exercise to lock in the structure. Three pieces of the skeleton are missing — fill each ___ using the hints.

    🎯 YOUR TURN: rebuild the skeleton
    // 🎯 YOUR TURN — three things are missing. Fill in each ___.
    
    // 1) Declare the runnable package
    ___ main          // 👉 the keyword that starts the file, then: main
    
    // 2) Import the printing package
    import "___"      // 👉 the standard package that holds Println
    
    // 3) Name the entry-point function Go runs first
    func ___() {      // 👉 the special function name Go looks for
        fmt.Println("It compiles!")
    }
    
    // ✅ Expected output:
    //    It compiles!
    Output
    It compiles!
    Fill in the three ___ blanks so the program compiles, then run it at the Go Playground.

    Mini-Challenge: Your "About Me" Card

    No blanks this time — just a brief and an outline to keep you on track. Write it yourself, run it, and check your output against the example in the comments. This is exactly the kind of tiny program that builds real fluency.

    🎯 MINI-CHALLENGE: print a three-line card
    package main
    
    import "fmt"
    
    func main() {
        // 🎯 MINI-CHALLENGE: An "about me" card
        // 1. Print a line with your name.
        // 2. Print a line with the year you started coding.
        // 3. Print a line with one thing you want to build.
        // (Three separate fmt.Println calls — text goes in "double quotes".)
        //
        // ✅ Example output:
        //    Name: Alex
        //    Coding since: 2026
        //    Goal: a command-line to-do app
    
        // your code here
    }
    Output
    Name: Alex
    Coding since: 2026
    Goal: a command-line to-do app
    Write the three print lines yourself, then run it at the Go Playground to check it.

    Common Errors (and the fix)

    • "imported and not used: fmt" — In Go an unused import is a compile error, not a warning. If you import a package you never call, delete the import. (Need it only for a side effect? Use the blank identifier: import _ "fmt".)
    • "declared and not used: x" — Same rule for variables: declaring one you never read is a compile error. Use it, or remove it. This strictness keeps dead code out of your programs.
    • "expected 'package', found ..." — Every file must begin with a package line, and a runnable program needs package main. Missing it (or misspelling it) stops compilation.
    • "undefined: Println" or it just won't run — Exported (callable from outside) names start with a capital letter. It's fmt.Println, never fmt.println. The capital P is what makes it public.
    • "syntax error: unexpected newline ..." — The opening brace must be on the same line: write func main() {, not func main() with the { on the next line.

    Pro Tips

    • 💡 Let the tools format for you: run go fmt (or save in an editor with Go support) and your code is auto-formatted to the one true style — no debates.
    • 💡 Use the Go Playground at go.dev/play to test snippets instantly without installing anything.
    • 💡 Embrace the strictness: Go refusing to compile unused imports and variables feels picky for an hour, then quietly keeps your projects clean forever.

    📋 Quick Reference — Go Basics

    ConceptGo Syntax
    Declare runnable packagepackage main
    Import a packageimport "fmt"
    Entry-point functionfunc main() { }
    Print a linefmt.Println("Hello")
    Print, no newlinefmt.Print("Hi")
    Run a filego run main.go
    Build a binarygo build -o app
    Check versiongo version

    Frequently Asked Questions

    Q: Why does every Go program need package main and func main()?

    Go organises all code into packages. The package named main is the only one Go can turn into a runnable program, and func main() is the exact function it calls first to start that program. Together they tell Go "this is an app you can run, and here is where it begins". A package without main can still be imported by other code, but you can't run it on its own.

    Q: What is the difference between fmt.Println and fmt.Print?

    fmt.Println ("print line") writes your text and then moves to a new line, and it puts a space between multiple values. fmt.Print writes the text with no trailing newline and no automatic spaces, so the next print continues on the same line. Use Println for most output; reach for Print when you want to build up a single line piece by piece.

    Q: Do I need to install anything to try these examples?

    No. You can paste any example into the free Go Playground at go.dev/play in your browser and click Run — no install required. When you're ready to build real projects on your own machine, install Go from go.dev and run files with the go run command.

    Q: Is Go hard to learn compared to other languages?

    Go is deliberately one of the simplest languages to learn. It has only 25 keywords, no complicated class hierarchies, and a built-in formatter (go fmt) that ends arguments about style. Its strictness — like refusing to compile unused imports — feels picky at first but quickly keeps your code clean and bug-free.

    🎉 Lesson Complete!

    • ✅ Go is compiled, fast, simple, and has built-in concurrency
    • package main marks a program you can run
    • import "fmt" brings in code for printing
    • func main() is where every program starts
    • fmt.Println prints a line; go run main.go runs your code
    • ✅ Unused imports and variables are compile errors — Go keeps your code clean
    • Next lesson: Variables and Data Types — store text, numbers, and true/false values

    Sign up for free to track which lessons you've completed and get learning reminders.

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service