Create a module
To create a Go module, you need to follow these steps:
Initialize a new module: Use the
go mod init
command to create a new module.Create packages: Organize your code into packages.
Use the module: Import and use the packages within your module.
Here are detailed examples:
Step 1: Initialize a New Module
First, create a new directory for your project and navigate into it. Then, initialize the module.
Step 2: Create Packages
Create a directory structure for your packages. For example, create two packages: math
and stringutil
.
Example: math
Package
Create a file math/math.go
:
Example: stringutil
Package
Create a file stringutil/stringutil.go
:
Step 3: Use the Module
Create a main.go
file in the root directory to use the packages.
Step 4: Run the Program
Run the program using the go run
command.
This will output:
By following these steps, you can create a Go module with multiple packages and use them in your project.