Chapter 1: Getting Started
Every Go program starts the same way: a package declaration, an import block, and a main function. If you can read those three things, you can read any Go program. Let’s get them under your fingers.
Package Name and Imports
The first line of a Go program declares which package this file belongs to:
1 package main
main is special — it tells Go this is an executable program, not a library. Every program you run has a main package.
Next, you import whatever you need. Go’s import works like Ruby’s require:
1 import "fmt"
For multiple imports, group them in parentheses:
1 import (
2 "fmt"
3 "os"
4 )
Unlike Ruby, where puts is available everywhere, Go makes imports explicit. If you want to print, you import fmt. No magic.
Println
Once fmt is imported, Println works like Ruby’s puts — it prints a line:
1 fmt.Println("Hello World")
No semicolons needed. Multiple values get separated by spaces automatically:
1 fmt.Println("Hello", "World")
Println appends a newline for you. But what if you need to interpolate values into a string?
Printf
For formatted output, use Printf — Go’s answer to Ruby’s string interpolation:
1 fmt.Printf("The number is %d\n", value)
The %d is a verb — a placeholder that tells Go what type to expect and how to print it. Verbs come from C. Here are the ones you’ll use most:
| verbs | purpose |
| %v | prints the value of a struct |
| %+v | prints a struct with field names |
| %t | prints a boolean (true/false) |
| %d | prints an integer (base 10) |
| %f, %F | prints a float without exponent |
| %s | prints a string |
Unlike Println, Printf doesn’t add a newline — add \n when you need one.
Sprintf
Sprintf formats a string but returns it instead of printing it. Use it when you need to build a string in pieces:
1 location := fmt.Sprintf("I live in %s.", myLocation())
2 date := fmt.Sprintf("My current date & time is %s.", myCurrentDate())
Then combine the pieces:
1 details := fmt.Sprintf("My name is %s. %s %s\n", name, location, date)
And print the result:
1 fmt.Printf("My Details: \n %s\n", details)
Putting It Together
Before going further, here is a complete Go program you can save and run:
1 package main
2
3 import "fmt"
4
5 func main() {
6 name := "Ruby developer"
7 greeting := fmt.Sprintf("Hello, %s. Welcome to Go.", name)
8 fmt.Println(greeting)
9 }
Save it as hello.go and run:
1 $ go run hello.go
2
3 Hello, Ruby developer. Welcome to Go.
This is the skeleton every Go program follows: package main, one or moreimport statements, a main function. Everything else is built on top of
this structure.
Functions
Go doesn’t have classes. Everything is a function. You’ll write a lot of them — here’s how.
Function Basics
A function starts with func, then the name, then parentheses and braces:
1 func say() {
2 fmt.Println("Hello World")
3 }
Function Parameters and Arguments
Add parameters by giving each one a name and a type. Unlike Ruby, types are mandatory:
1 func say(name string) {
2 fmt.Println("Hello", name)
3 }
4
5 say("World")
Multiple parameters separate with commas. When consecutive parameters share a type, you can shorten the syntax:
1 func say(greeting, name string) {
2 fmt.Println(greeting, name)
3 }
4
5 say("Hello", "World")
Function Return Types
Since Go is typed, you declare what a function returns by putting the type after the parameter list:
1 func say(greeting, name string) string {
2 return fmt.Sprintln(greeting, name)
3 }
Now say returns a string, which you can pass directly to Println:
1 fmt.Println(say("Hello", "World"))
Main Function
The main function is where your program starts. It takes no arguments and returns nothing:
1 func main() {}
Everything inside main runs when you execute the binary:
1 func say(greeting, name string) string {
2 return fmt.Sprintln(greeting, name)
3 }
4
5 func main() {
6 fmt.Println(say("Hello", "World"))
7 }
Chapter Exercises
Summary
You can now read and write basic Go programs. Every Go file starts with package main. Imports pull in functionality the way require does in Ruby. fmt.Println and fmt.Printf replace puts and string interpolation. And func main() is where your program starts — always.
These building blocks never change. Come back here when you need a refresher.
In the next chapter, you’ll learn how Go organizes code into packages and manages dependencies with Go Modules — the equivalent of RubyGems and Bundler, built into the language.