Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Go Recipes for Developers
  • Table Of Contents Toc
  • Feedback & Rating feedback
Go Recipes for Developers

Go Recipes for Developers

By : Burak Serdar
close
close
Go Recipes for Developers

Go Recipes for Developers

By: Burak Serdar

Overview of this book

With its simple syntax and sensible conventions, Go has emerged as the language of choice for developers in network programming, web services, data processing, and other settings. This practical guide helps engineers leverage Go through up-to-date recipes that solve common problems in day-to-day programming. Drawing from three decades of distributed systems engineering and technical leadership at companies like Red Hat, Burak Serdar brings battle-tested expertise in building robust, scalable applications. He starts by covering basics of code structure, describing different approaches to organizing packages for different types of projects. You’ll discover practical solutions to engineering challenges in network programming, dealing with processes, databases, data processing pipelines, and testing. Each chapter provides working solutions and production-ready code snippets that you can seamlessly incorporate into your programs while working in sequential and concurrent settings. The solutions leverage the more recent additions to the Go language, such as generics and structured logging. Most of the examples are developed using the Go standard library without any third-party packages. By the end of this book, you’ll have worked through a collection of proven recipes that will equip you accelerate your Go development journey.
Table of Contents (20 chapters)
close
close

Creating a module

When you start working on a new project, the first thing to do is to create a module for it. A module is how Go manages dependencies.

How to do it...

  1. Create a directory to store a new module.
  2. Under that directory, use go mod init <moduleName> to create the new module. The go.mod file marks the root directory of a module. Any package under this directory will be a part of this module unless that directory also has a go.mod file. Although such nested modules are supported by the build system, there is not much to be gained from them.
  3. To import a package in the same module, use moduleName/packagePath. When moduleName is the same as the location of the module on the internet, there are no ambiguities about what you are referring to.
  4. For the packages under a module, the root of the module is the closest parent directory containing a go.mod file. All references to other packages within a module will be looked up in the directory tree under the module root.
  5. Start by creating a directory to store the project files. Your current directory can be anywhere on the filesystem. I have seen people use a common directory to store their work, such as $HOME/projects (or \user\myUser\projects in Windows). You may choose to use a directory structure that looks like the module name, such as $HOME/github.com/mycompany/mymodule (or \user\myUser\github.com\mycompany\mymodule in Windows). Depending on your operating system, you may find a more suitable location.

Warning

Do not work under the src/ directory of your Go installation. That is the source code for the Go standard library.

Tip

You should not have an environment variable, GOPATH; if you have to keep it, do not work under it. This variable was used by an older mode of operation (Go version <1.13) that is now deprecated in favor of the Go module system.

Throughout this chapter, we will be using a simple program that displays a form in a web browser and stores the entered information in a database.

After creating the module directory, use go mod init. The following commands will create a webform directory under projects and initialize a Go module there:

$ cd projects
$ mkdir webform
$ go mod init github.com/examplecompany/webform

This will create a go.mod file in this directory that looks like this:

module github.com/PacktPublishing/Go-Recipes-for-Developers/chapter1/webform
go 1.21.0

Use a name that describes where your module can be found. Always use a URL structure such as the <host>.<domain>/location/to/module format (e.g., github.com/bserdar/jsonom). In particular, the first component of the module name should have a dot (.) (the Go build system checks this).

So, even though you can name the module something such as webform or mywork/webform, do not do so. However, you can use something such as workspace.local/webform. When in doubt, use the code repository name.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Go Recipes for Developers
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon