Reader small image

You're reading from  Go Programming - From Beginner to Professional - Second Edition

Product typeBook
Published inMar 2024
Reading LevelBeginner
PublisherPackt
ISBN-139781803243054
Edition2nd Edition
Languages
Right arrow
Author (1)
Samantha Coyle
Samantha Coyle
author image
Samantha Coyle

Samantha Coyle, a Software Engineer at Diagrid, specializes in Go for cloud-native developer tooling, abstracting application development challenges. Committed to Open Source, she contributes to projects like Dapr and Testcontainers. She boasts a rich history in retail computer vision solutions and successfully stabilized industrial edge use cases with testing and diverse deployments for biopharma data pipelines. Her expertise extends to being CKAD certified and reviewing Go textbooks. She is passionate about empowering early-career, diverse professionals. Samantha is in a family of gophers, and enjoys GopherCon with her brother and identical twin sister. She's a seasoned speaker, having presented at various conferences, including GopherCon.
Read more about Samantha Coyle

Right arrow

Preface

Welcome to Go Programming - From Beginner to Professional! Here, you will learn everything you need to build modern software utilizing Go. Designed for beginners with no prior programming experience required, it offers a comprehensive approach to understanding and leveraging Go’s power and idiomatic nature.

The best way to learn is by doing. You will do exactly that in this book. Go Programming – From Beginner to Professional will take you on an engaging step-by-step journey to understanding Go starting with the fundamentals. Each chapter includes exciting exercises and activities that you can do at your own pace or jump ahead to. As you continue reading, you will work your way to more advanced topics where you will discover how to harness Go’s efficiency, simplicity, and concurrency to build robust and scalable software solutions. Learning at your own terms and pace, you will build up and reinforce key skills in a way that feels rewarding to you as you grow as a Go developer.

This book will highlight the latest features of Go 1.21 and onward, ensuring you’re up-to-date with the cutting-edge capabilities of this versatile language as you grow your skills. You will build and iterate on your code like a Go Gopher does, learning along the way.

Join in on this exciting adventure as we unlock the full potential of Go and empower you to become a proficient Go developer. Whether you are building web applications, microservices, or tackling general software challenges and want to leverage Go, this book equips you with the knowledge and skills to succeed. Let us dive in and elevate your programming journey with Go.

Who this book is for

Designed for newcomers to Go, whether starting from scratch or transitioning from another language, this book empowers developers to build real-world projects and develop the necessary skills to launch their careers in Go. Offering a step-by-step approach, beginners can grasp the Go fundamentals even without prior programming experience. As readers progress, they uncover idiomatic Go best practices and explore the latest features of the language. Readers will gain expertise in building modern software in Go, equipping them to become professional developers through hands-on learning experiences.

What this book covers

Chapter 1, Variables and Operators, explains how variables hold data for you temporarily. It also shows how you can use operators to make changes or make comparisons to that data.

Chapter 2, Command and Control, teaches you how to make your code dynamic and responsive by creating rules that must be followed based on data in variables. Loops let you repeat logic over as you learn to take command of your control flow with Go.

Chapter 3, Core Types, introduces you to the building blocks of data. You’ll learn what a type is and how the core types are defined.

Chapter 4, Complex Types, explains that complex types build on core types to allow you to model real-world data using data grouping and by composing new types from the core types. You’ll also look at overcoming Go’s type system when needed.

Chapter 5, Functions – Reduce, Reuse, and Recycle, teaches you the basics of constructing a function. Then, we will dive into more advanced features of using functions, such as passing a function as an argument, returning a function, assigning a function to a variable, and many more interesting things you can do with functions. You will learn the fundamentals of code reuse.

Chapter 6, Don’t Panic! Handle Your Errors, teaches you how to work with errors, covering topics such as declaring your own error and handling errors the Go way. You will learn what a panic is and how to recover from one.

Chapter 7, Interfaces, starts by teaching the mechanics of interfaces and then demonstrates that interfaces in Go offer polymorphism, duck typing, the ability to have empty interfaces, and the implicit implementation of an interface.

Chapter 8, Generic Algorithm Superpowers, showcases the type parameter syntax Go offers to create a generic version of your code that is usable on more than one type. You will understand the when, why, and how to utilize generics to reduce code duplication.

Chapter 9, Using Go Modules to Define a Project, demonstrates how to leverage Go modules to structure and manage Go projects, covering essential Go dependency management files.

Chapter 10, Package Keep Projects Manageable, demonstrates how to leverage Go packages within our programs to keep code manageable and group code into useful subsystems of functionality.

Chapter 11, Bug-Busting Debugging Skills, teaches the fundamentals of finding bugs in our application. You will use various techniques of printing out markers in code, using values and types, and performing logging.

Chapter 12, About Time, gets you a head start in the concept of how Go manages time variables, and what features are provided for you to improve your applications, such as measuring execution time and navigating between time zones.

Chapter 13, Programming from the Command Line, teaches you how to create command line utilities with all that Go has to offer. You will practice flag parsing, handling large amounts of data, exit codes, terminal user interfaces, and learn best practices along the way.

Chapter 14, Files and Systems, shows how Go has great support for working with files and the underlying OS. You will be working with the filesystem, learning how to create, read, and modify files on the OS. You will also see how Go can read a CSV file, a common file format used by administrators.

Chapter 15, SQL and Databases, covers the most important aspects of connecting to databases and manipulating tables, which are very common tasks nowadays, and you’ll learn how to work efficiently with databases using Go.

Chapter 16, Web Servers, teaches you how to use the Go standard packages to create an HTTP server, build websites, and create REST APIs You’ll learn how to accept requests from a web form or from another program and respond in a human or machine readable format.

Chapter 17, Using the Go HTTP Client, instructs you how to use the Go standard packages to create an HTTP client and interact with REST APIs. You’ll learn how to send GET requests to a server and process the response, as well as how to POST form data to a server and how to upload a file to a server.

Chapter 18, Concurrent Work, demonstrates how to make use of Go’s concurrency features to enable your software to perform several tasks at the same time, splitting the work across independent Goroutines and reducing the processing time.

Chapter 19, Testing, helps understand the various types of testing that Go enables to include HTTP testing, fuzz testing, benchmark testing, using test suites, and generating test reports and code coverage of your code.

Chapter 20, Using Go Tools, familiarizes you with the tools that come with Go and explains how you can use them to improve your code. You’ll learn how to automatically format your code with gofmt and goimports. You’ll also learn how to do static analysis with go vet and how to detect race conditions using the Go race detector.

Chapter 21, Go in the Cloud, builds your understanding of preparing your Go code for deployment in the cloud. You will work through adding monitoring capabilities using tools such as Prometheus, OpenTelemetry, and how to containerize your Go application to work with orchestrators such as Kubernetes.

To get the most out of this book

Each great journey begins with a humble step. Our upcoming adventure with Go programming is no exception. Before we can do awesome things using Go, we need to be prepared with a productive environment. For this book to best serve you, you should install Git, Docker, and Go version 1.21 or higher. It is recommended that you have 4GB of RAM, and virtualization enabled in BIOS (usually enabled by default). The book is best suited for macOS or Linux, and will require minor tweaks for using Windows equivalent commands if needed. It is recommended to use a 1.6 GHz or faster desktop processor.

A helping hand on additional setup:

Install the Go Compiler

To turn your Go source code into something you can run, you’ll need the Go compiler. For Windows and macOS, we recommend using the installer. Alternatively, to get more control you can download precompiled binaries. You can find both at https://packt.live/2PRUGjp. The install instructions for both methods on Windows, macOS, and Linux are at https://packt.live/375DQDA. The Go compiler is free to download and use.

Install Git

Go uses the version control tool Git to install extra tools and code. You can find the instructions for Windows, macOS, and Linux at https://packt.live/35ByRug. Git is free to install and use.

Install Visual Studio Code (Editor/IDE)

You need something to write your Go source code. This tool is called an editor or an Integrated Development Environment (IDE). If you already have an editor you like, you can use it with this course if you’d like to.

If you don’t already have an editor, we recommend you use the free editor Visual Studio Code. You can download the installer from https://packt.live/35KD2Ek:

  1. Once it’s installed, open Visual Studio Code.
  2. From the top menu bar, select View.
  3. From the list of options, select Extensions.
  4. A panel should appear on the left side. At the top is a search input box. Type Go.
  5. The first option should be an extension called Go by Microsoft.
  6. Click the Install button on that option.
  7. Wait for a message that says it’s successfully installed.

If you have Git installed, follow these steps:

  1. Press Ctrl/Cmd + Shift + P all at the same time. A text input should appear at the top of the window.
  2. Type go tools.
  3. Select the option labelled something like Go: Install/Update Tools.
  4. You’ll see a list of options and checkboxes.
  5. The very first checkbox next to the search input checks all the checkboxes. Select this checkbox, then select the Go button to the right of it.
  6. A panel from the bottom should appear with some activity in it. Once this stops (and it may take a few minutes), you’re all done.

Once done, select View from the top menu bar, then select Explorer.

Install Docker

Docker allows us to run things such as database servers without having to install them and containerize our applications. Docker is free to install and use.

For macOS users, follow the instructions at https://packt.live/34VJLJD.

For Windows users, follow the instructions at https://packt.live/2EKGDG6.

Linux users, you should be able to use your built-in package manager to install Docker. Instructions for common distributions are at https://packt.live/2Mn8Cjc.

You are safe to uninstall Docker, if you wish, once the book is complete.

Install PostgreSQL

PostgreSQL is used within the chapter covering database interactions. To install the PostgreSQL driver, follow the instructions at https://www.postgresql.org/download/.

If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

Download the example code files

You can download the example code files for this book from GitHub at https://packt.link/sni2F. If there’s an update to the code, it will be updated in the GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Conventions used

There are a number of text conventions used throughout this book.

Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: “In the previous chapter, we learned how to use if, if-else, else-if, switch, case, continue, break, and goto in Go.”

A block of code is set as follows:

package main
import "fmt"
func main() {
  fmt.Println(10 > 5)
  fmt.Println(10 == 5)
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

go doc -all

Any command-line input or output is written as follows:

error, unexpected nil value

Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: “ Once done, select View from the top menu bar, then select Explorer.”

Tips or important notes

Appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, email us at customercare@packtpub.com and mention the book title in the subject of your message.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Share Your Thoughts

Once you’ve read Go Programming - From Beginner to Professional, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

Download a free PDF copy of this book

Thanks for purchasing this book!

Do you like to read on the go but are unable to carry your print books everywhere?

Is your eBook purchase not compatible with the device of your choice?

Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.

The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily

Follow these simple steps to get the benefits:

  1. Scan the QR code or visit the link below

https://packt.link/free-ebook/9781803243054

  1. Submit your proof of purchase
  2. That’s it! We’ll send your free PDF and other benefits to your email directly
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Go Programming - From Beginner to Professional - Second Edition
Published in: Mar 2024Publisher: PacktISBN-13: 9781803243054
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Samantha Coyle

Samantha Coyle, a Software Engineer at Diagrid, specializes in Go for cloud-native developer tooling, abstracting application development challenges. Committed to Open Source, she contributes to projects like Dapr and Testcontainers. She boasts a rich history in retail computer vision solutions and successfully stabilized industrial edge use cases with testing and diverse deployments for biopharma data pipelines. Her expertise extends to being CKAD certified and reviewing Go textbooks. She is passionate about empowering early-career, diverse professionals. Samantha is in a family of gophers, and enjoys GopherCon with her brother and identical twin sister. She's a seasoned speaker, having presented at various conferences, including GopherCon.
Read more about Samantha Coyle