Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Go CookBook

You're reading from   Go CookBook Top techniques and practical solutions for real-life Go programming problems

Arrow left icon
Product type Paperback
Published in Dec 2024
Publisher Packt
ISBN-13 9781835464397
Length
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Burak Serdar Burak Serdar
Author Profile Icon Burak Serdar
Burak Serdar
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Chapter 1: Project Organization 2. Chapter 2: Working with Strings FREE CHAPTER 3. Chapter 3: Working with Date and Time 4. Chapter 4: Working with Arrays, Slices, and Maps 5. Chapter 5: Working with Types, Structs, and Interfaces 6. Chapter 6: Working with Generics 7. Chapter 7: Concurrency 8. Chapter 8: Errors and Panics 9. Chapter 9: The Context Package 10. Chapter 10: Working with Large Data 11. Chapter 11: Working with JSON 12. Chapter 12: Processes 13. Chapter 13: Network Programming 14. Chapter 14: Streaming Input/Output 15. Chapter 15: Databases 16. Chapter 16: Logging 17. Chapter 17: Testing, Benchmarking, and Profiling 18. Index 19. Other Books You May Enjoy

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.

lock icon The rest of the chapter is locked
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.
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 $19.99/month. Cancel anytime
Banner background image