Prerequisites
Before you begin, make sure you have:
Go installed on your system (installation guide )
A text editor or IDE
Basic familiarity with command-line interfaces
Your first Go program
Create a new directory
Create a directory for your Go project: mkdir hello-go
cd hello-go
Initialize a Go module
Initialize a new Go module: go mod init example.com/hello
This creates a go.mod file that tracks your project’s dependencies.
Create your program
Create a file named main.go with the following content: package main
import " fmt "
func main () {
fmt . Println ( "Hello, Go!" )
}
Run your program
Execute your program: You should see:
Build an executable
Compile your program into an executable: This creates an executable file you can run directly: ./hello # On Unix/Linux/macOS
hello.exe # On Windows
Working with packages
Let’s enhance your program by using Go’s standard library:
package main
import (
" fmt "
" time "
)
func main () {
fmt . Println ( "Hello, Go!" )
fmt . Println ( "The current time is:" , time . Now (). Format ( "15:04:05" ))
}
Run it again:
Using external packages
Add an external package to fetch data from the web:
Add a dependency
Go automatically downloads dependencies when you import them: package main
import (
" encoding/json "
" fmt "
" io "
" net/http "
)
type Response struct {
Message string `json:"message"`
}
func main () {
resp , err := http . Get ( "https://httpbin.org/json" )
if err != nil {
fmt . Println ( "Error:" , err )
return
}
defer resp . Body . Close ()
body , err := io . ReadAll ( resp . Body )
if err != nil {
fmt . Println ( "Error reading body:" , err )
return
}
fmt . Println ( "Response:" , string ( body ))
}
Run with automatic dependency management
Go will automatically download and manage dependencies: Go downloads the required packages and runs your program.
View dependencies
Check your go.mod file to see tracked dependencies:
Essential Go commands
Here are the most common Go commands you’ll use:
Command Description go run main.goCompile and run your program go buildCompile into an executable go mod init <module>Initialize a new module go mod tidyAdd missing and remove unused dependencies go get <package>Add a dependency go testRun tests go fmtFormat your code go vetExamine code for common mistakes
Common patterns
Variables and types
// Short variable declaration (inside functions)
name := "Alice"
age := 30
// Explicit type declaration
var count int = 10
var price float64 = 99.99
// Multiple variables
x , y := 1 , 2
Functions
func add ( a , b int ) int {
return a + b
}
func divide ( a , b float64 ) ( float64 , error ) {
if b == 0 {
return 0 , fmt . Errorf ( "division by zero" )
}
return a / b , nil
}
Error handling
result , err := divide ( 10 , 0 )
if err != nil {
fmt . Println ( "Error:" , err )
return
}
fmt . Println ( "Result:" , result )
Structs
type Person struct {
Name string
Age int
}
person := Person {
Name : "Bob" ,
Age : 25 ,
}
fmt . Println ( person . Name )
Slices and maps
// Slice (dynamic array)
numbers := [] int { 1 , 2 , 3 , 4 , 5 }
numbers = append ( numbers , 6 )
// Map (hash table)
ages := map [ string ] int {
"Alice" : 30 ,
"Bob" : 25 ,
}
ages [ "Charlie" ] = 35
Go has built-in code formatting. Always format your code:
This ensures your code follows Go’s standard formatting conventions.
Most Go editors and IDEs automatically run go fmt when you save a file.
Next steps
Complete tutorial Follow a comprehensive tutorial covering all Go fundamentals
Language specification Read the official Go language specification
Standard library Explore Go’s rich standard library
Effective Go Learn Go best practices and idioms