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

Using Go Modules to Define a Project

Overview

This chapter dives into the use of Go modules for structuring and managing Go projects. We will start by introducing the concept of modules and their significance in organizing code. This chapter will also cover creating your first module while discussing the essential go.mod and go.sum files.

Moreover, we will cover how to use third-party modules as dependencies and offer insights into managing these dependencies effectively. This chapter will provide hands-on experience through exercises and activities that will empower you to develop more structured and manageable Go projects, promoting code reusability and simplifying the development process.

Technical requirements

For this chapter, you'll require Go version 1.21 or higher. The code for this chapter can be found at: https://github.com/PacktPublishing/Go-Programming-From-Beginner-to-Professional-Second-Edition-/tree/main/Chapter09.

Introduction

In the previous chapter, we learned about the importance of creating maintainable, reusable, and modular software using Go packages. We learned how packages are structured, the principles of proper package naming, and the distinctions between executable and non-executable packages. The concept of exportable and unexportable code was also discussed.

In this chapter, we will expand upon this knowledge and explore the utilization of Go modules to define projects, advancing our software development capabilities. We will understand what Go modules are, how they are helpful, and even create our own module. We will understand the different files required for working with Go modules to maintain the integrity of our project dependencies, and then learn how to consume third-party modules and manage them. Lastly, we will look at how to create a project containing multiple modules, and when that is useful.

What is a module?

In the world of Go programming, a module is a fundamental concept that serves as a cornerstone for organizing, versioning, and managing your projects and their dependencies. Think of it as a self-contained, encapsulated unit that simplifies the complexities of dependency management while fostering code reusability and maintainability.

A Go module represents a discrete collection of Go packages, all neatly bundled together under a common, versioned umbrella. This isolation ensures that your code base remains cohesive and well-structured, making it easier to share, collaborate on, and maintain. Modules are designed to put you in control of your project’s external dependencies and provide a structured mechanism for versioning and managing them.

Key components when working with Go modules

There are a few key components that are associated with working Go modules. Let’s take a look at some of the aspects that help us with Go dependency management...

How are modules helpful?

Go modules offer many benefits that enhance the Go development experience. Let’s take a closer look at how Go modules are helpful.

Precise and simplified dependency management

One of the most significant advantages of Go modules is their ability to provide precise control over dependencies. When specifying dependencies in your go.mod file, you can define the exact versions you need, which eliminates the guesswork and potential compatibility issues associated with less rigorous dependency management methods.

Go modules have streamlined the process of adding, updating, and managing dependencies. In the past, Go developers had to rely on the GOPATH and vendor directories, which could lead to version conflicts and make it challenging to manage dependencies. Go modules replace these practices with a more intuitive and efficient approach.

Versioning and reproducibility

Go modules introduce a robust versioning system. Each module is tagged with...

Exercise 09.01 – creating and using your first module

In this exercise, we will see how to create our first Go module with ease:

  1. Create a new directory called bookutil and navigate into it:
    mkdir bookutil
    cd bookutil
  2. Initialize a Go module named bookutil:
    go mod init bookutil
  3. Verify that go.mod is created within your project directory with the module path set as bookutil.

Note

No go.sum file is created after running go mod init. It will be generated and updated as you interact with your module and add to its dependencies.

  1. Now, let’s create a Go package for the author information while focusing on functions related to book chapters by creating a directory named author within our module’s project directory.
  2. Inside the author directory, create a file named author.go to define the package and functions.

    Here is the starting code for author.go:

    package author
    import "fmt"
    // Author represents an author of a book.
    type Author...

When should you use external modules, and why?

In Go development, leveraging external modules is a frequent practice that can benefit your projects. External modules, also known as third-party dependencies, offer many advantages when used judiciously. In this section, we will explore when to use external modules and the compelling reasons behind their adoption.

You should use external modules to do the following:

  • Promote code reusability and efficiency
  • Expand project functionality
  • Offload dependency management
  • Enable collaborative development with the open source community
  • Utilize proven reliability, community support, and documentation by open source code

However, always exercise caution and select dependencies and modules that align with your project’s goals and long-term sustainability plans.

Exercise 09.02 – using an external module within our module

Sometimes, in code, you need a unique identifier for an identity you provide to something. This unique identifier is often called a universally unique identifier (UUID). Google provides a package to create such a UUID. Let’s look at how to use it:

  1. Create a new directory called myuuidapp and navigate into it:
    mkdir myuuidapp
    cd myuuidapp
  2. Initialize a Go module named myuuidapp:
    go mod init myuuidapp
  3. Verify that the go.mod file is created within your project directory with the module path set as myuuidapp.
  4. Add a main.go file.
  5. In main.go, add the main package name to the top of the file:
    package main
  6. Now, add the imports we will use in this file:
    import (
        "fmt"
        "github.com/google/uuid"
    )
  7. Create the main() function:
    func main() {
  8. Generate a new UUID using the external module package:
        id := uuid.New...

Consuming multiple modules within a project

You can consume multiple Go modules within a project. Just as you saw with the Google module example earlier, you can use that module alongside other Go modules that you may need in your project.

Activity 9.01 – consuming multiple modules

In this activity, we will use multiple Go modules within our code:

  1. Create a new UUID and print the UUID.
  2. Fetch and print a random quote using the rsc.io/quote module.

Your output should look like this, with a different UUID and a different random sentence for your second line:

Generated UUID: 3c986212-f12d-415e-8eb5-87f61a6cbfee
Random Quote: Do not communicate by sharing memory, share memory by communicating.

Note

The solution for this activity can be found in the GitHub repository folder for this chapter: https://github.com/PacktPublishing/Go-Programming-From-Beginner-to-Professional-Second-Edition-/tree/main/Chapter09/Activity09.01.

Defining multiple modules within a project

The Go module system is designed to manage dependencies and versions for the entire module, not for subsets or subprojects within a module. However, there might be situations where you have multiple distinct components or subprojects within your main project, and each of these components or subprojects has dependencies and version requirements. In such cases, you can structure your project in a way that each component is its own module, separate from the main project module. These submodules can be maintained as separate Go modules, each with its own go.mod file.

For example, if you have a project with a main component and two other components, and each component has unique dependencies, you can structure your project like this:

myproject/
├── mainmodule/
│   ├── main.go
│   ├── go.mod
│   ├─...

Go workspaces

In Go 1.18, the Go workspaces feature was released, which improved the experience of working with multiple Go modules within the same project locally. Originally, when working with multiple Go modules in the same project, you would need to manually edit the Go module files for each module with the replace directive to use your local changes. Now, with Go workspaces, we can define a go.work file, specifying to use our local changes, and not have to worry about managing several go.mod files manually ourselves. This is particularly useful when working with larger projects, or projects that span multiple repositories.

Exercise 09.03 – working with workspaces

In this exercise, we will look at what it used to be like when working with projects that had multiple Go modules that needed their dependencies to be replaced so that they could use local changes. We will then update the example code so that it uses Go workspaces to show the improvements:

  1. Create...

Summary

In this chapter, we explored the world of Go modules, beginning with an understanding of what modules are and how they provide structured project organization and management of project dependencies. We introduced the two key module files – go.mod and go.sum – that handle dependencies. We also delved into external modules, emphasizing their role in expanding project functionality and their impact on maintainability. We discussed using and consuming multiple modules in a single project, and the concept of Go workspaces for managing multiple modules within a project directory. Hands-on exercises and activities reinforced our understanding.

In the next chapter, we will enhance our module understanding by covering how packages help keep projects more manageable for teams as they iterate, reuse, and maintain projects using packages.

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