Go Getting Started – Setup, Syntax, Output & Basics
Introduction – Why Start with Go?
Go (or Golang), developed by Google, is a modern, statically typed language known for its simplicity, speed, and built-in concurrency. Whether you’re writing web servers, APIs, or CLI tools, Go provides a clean and productive foundation for scalable software.
In this guide, you’ll learn:
- How to install and set up Go
- The structure of a Go program
- Go’s syntax, output methods, comments, and import system
- How Go handles variables and default (zero) values
Topics Covered
| Topic | Description |
|---|---|
| Go – Home | Welcome to Go programming and its ecosystem |
| Go – Introduction | Why Go was created, its features and use cases |
| Go – Environment Setup | Step-by-step installation and workspace configuration |
| Go – Get Started | Program structure and execution basics |
| Go – Basic Syntax | Syntax rules, semicolons, identifiers |
| Go – Output | Using fmt to print data to the console |
| Go – Comments | Writing single-line and multi-line comments |
| Go – Fmt Package | Standard package for formatted I/O operations |
| Go – Zero Value | Default values for uninitialized variables |
| Go – Import Statement | How to include packages in your Go programs |
Go – Home
Go is a compiled, open-source language designed for productivity and performance. It compiles quickly, has garbage collection, and supports first-class concurrency using goroutines.
Go is best for:
- Web backends and APIs
- Networking tools
- Cloud-native microservices
Go – Introduction / Overview
Key Features:
- Simple syntax like C, but safer
- Fast compilation and execution
- Concurrency via goroutines
- Powerful standard library
Created by: Robert Griesemer, Rob Pike, and Ken Thompson
First released: 2009
Go – Environment Setup
Install Go:
On Windows/macOS/Linux:
- Download installer: https://go.dev/dl/
- Install and set
PATH - Verify in terminal:
go version
Example Output:
go version go1.22.0 linux/amd64
Go – Get Started / Program Structure
Hello World
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
Breakdown:
package main: Entry pointimport "fmt": Import standard formatting libraryfunc main(): Main function executed at runtime
Run it:
go run main.go
Go – Basic Syntax
- Statements end without
;(optional unless multiple statements on one line) - Uses braces
{}for blocks - Variable declaration:
var x int = 10or short form:x := 10
Example:
x := 42
fmt.Println(x)
Go – Output
Use the fmt package for printing:
fmt.Print("Hello ")
fmt.Println("World!") // Adds newline
fmt.Printf("Age: %d\n", 25) // Formatted output
Format specifiers:
%d: Integer%s: String%f: Float
Go – Comments
// This is a single-line comment
/*
This is a
multi-line comment
*/
Good for documentation and explanation within code.
Go – Fmt Package
The fmt package provides formatted I/O. Common functions:
| Function | Use Case |
|---|---|
Print() | Print without newline |
Println() | Print with newline |
Printf() | Formatted print |
Sprintf() | Returns formatted string instead of printing |
Example:
msg := fmt.Sprintf("Name: %s", "Alice")
fmt.Println(msg)
Go – Zero Value
In Go, uninitialized variables are given zero values:
var a int
var b string
var c bool
fmt.Println(a) // 0
fmt.Println(b) // ""
fmt.Println(c) // false
This ensures variables always have predictable default values.
Go – Import Statement
Imports are declared at the top using:
import "fmt"
Multiple imports:
import (
"fmt"
"math"
)
Aliasing:
import m "math"
fmt.Println(m.Pi)
Summary – Recap & Next Steps
Go makes it easy to get started with a minimal syntax, rich standard library, and a blazing-fast compiler. With just a few lines, you can write safe, concurrent, and production-ready applications.
Key Takeaways:
- Install Go using
go.dev/dland verify withgo version - Go programs start with
package mainandfunc main() - Use
fmtfor all output tasks - Uninitialized variables have zero values by default
- Import packages with
import "pkgname"or groupedimport ()
Real-World Use Cases:
- Microservices and REST APIs
- DevOps and networking tools
- Data pipelines and CLI utilities
Frequently Asked Questions
Is Go compiled or interpreted?
Go is a compiled language—it converts source code into native machine binaries using go build.
Do I need a special IDE to write Go?
No. Use any text editor or IDE (like VS Code with Go plugin). Go includes its own formatting and linting tools.
What is the default value of an uninitialized string in Go?
An empty string "".
How do I print variables in Go?
Use fmt.Printf():
name := "John"
fmt.Printf("Hello, %s!\n", name)
Can I use Go without installing it?
Yes. Try Go Playground to run code online.
Share Now :
