2 Go Crash Course
This chapter covers
- Packages and modules.
- Variables and pointers.
- Collection types: arrays, slices, and maps.
- Object-oriented programming.
- Concurrent programming.
- Error handling.
This chapter is a fast-paced primer for experienced programmers to prepare for the more advanced content we cover in the book. While other chapters focus on idiomatic Go and testability, this chapter focuses on the basics of Go's distinctive features and mechanics.
Treat this chapter as a warm-up rather than a mastery. Use it as reference material and return to it as you read the book. And don't worry if some of the content in this chapter is challenging to understand, as it'll become clearer as you progress in the book.
Note
The source code of this chapter is at the link: https://github.com/inancgumus/gobyexample/tree/main/basics
2.1 Packages
We'll start with a simple executable program example that prints this book's title.
This is our program's directory structure:
. -> The project's root directory (new directory) ├── go.mod -> Defines our project's name and dependencies ├── hello.go -> Implements the package main └── book -> The book package's directory └── book.go -> Implements the book package
Let's create a new directory on our system. We can create the rest later.
2.1.1 Overview
In Go, we write our code inside packages. There are two kinds of packages: