Go makes it easy to create reusable modules that you can import into other Go projects. In this guide, we will walk through how to create a Math Utilities module that offers basic mathematical operations, and then we'll use this module in a separate Go application.. In this blog, we'll explore the primary data types available in Go, providing examples and explanations along the way.
What You’ll Learn:
- Create a Go module — Build a small library module that provides mathematical functions.
- Call your module from another project — Use the math module in a separate Go application.
- Return random numbers — Generate random numbers and use slices in Go.
- Compile and run your Go application — Learn how to run your Go modules locally.
Step 1: Create a Math Utilities Module
First, we’ll create a Go module that provides two simple operations: Add and Multiply. You can expand on these later with other mathematical functions.
1.1 Create the Module Directory
Create a directory for the math utilities module:
mkdir mathutils
cd mathutils
1.2 Initialize the Module
Next, initialize the module with the following command:
go mod init example.com/mathutils
This creates a go.mod
file to manage dependencies and specify the module’s name.
1.3 Write the Math Utilities Code
In the mathutils
directory, create a new file called math.go
and write the following code:
package mathutils
// Add takes two integers and returns their sum.
func Add(a, b int) int {
return a + b
}
// Multiply takes two integers and returns their product.
func Multiply(a, b int) int {
return a * b
}
In this example, we define two simple mathematical functions: Add
and Multiply
, each taking two integer inputs and returning the result.
Step 2: Call Your Module From Another Project
Now, we’ll create a separate project that imports and uses the mathutils
module.
2.1 Create the Caller Directory
Navigate back to your workspace and create a directory for your new caller application:
cd ..
mkdir calculator
cd calculator
2.2 Initialize the Caller Module
Initialize the new caller module with:
go mod init example.com/calculator
2.3 Write the Caller Code
In the calculator
directory, create a file called main.go
and write the following code:
package main
import (
"fmt"
"example.com/mathutils"
)
func main() {
a, b := 10, 5
fmt.Printf("Add: %d + %d = %d\n", a, b, mathutils.Add(a, b))
fmt.Printf("Multiply: %d * %d = %d\n", a, b, mathutils.Multiply(a, b))
}
In this program, we import the mathutils
package and use the Add
and Multiply
functions to perform calculations on two integers.
Step 3: Link the Two Modules Locally
Since the mathutils
module is on your local machine and hasn’t been published online, we need to link it manually.
3.1 Edit the go.mod
File
In the calculator
directory, run the following command to tell Go to use the local version of mathutils
:
go mod edit -replace example.com/mathutils=../mathutils
This command adds a replace
directive to the calculator
module’s go.mod
file. After running it, the file should look like this:
module example.com/calculator
go 1.16
replace example.com/mathutils => ../mathutils
This directive tells Go to find example.com/mathutils
in the ../mathutils
directory rather than looking for it online.
3.2 Sync Dependencies
Run the following command to ensure that all dependencies are tracked correctly:
go mod tidy
After running this, your go.mod
file will also include a require
directive for the mathutils module:
module example.com/calculator
go 1.16
replace example.com/mathutils => ../mathutils
require example.com/mathutils v0.0.0-00010101000000-000000000000
Step 4: Run Your Application
With everything set up, you can now run the calculator
program. In the calculator
directory, run:
go run .
You should see the following output:
Add: 10 + 5 = 15
Multiply: 10 * 5 = 50
Congrats! You’ve successfully built and linked two Go modules.
Step 5: Adding Random Numbers (Optional)
Let’s take it a step further by adding a function that generates random numbers within a specified range. This will also introduce Go’s math/rand
package.
5.1 Modify the Math Utilities Module
Update math.go
in the mathutils
module to include a new Random
function:
package mathutils
import (
"math/rand"
"time"
)
// Add takes two integers and returns their sum.
func Add(a, b int) int {
return a + b
}
// Multiply takes two integers and returns their product.
func Multiply(a, b int) int {
return a * b
}
// Random generates a random number between min and max.
func Random(min, max int) int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(max-min+1) + min
}
The Random
function generates a random number between the min
and max
values.
5.2 Use the Random Function in the Caller Application
Modify the main.go
file in the calculator
module to use the new Random
function:
package main
import (
"fmt"
"example.com/mathutils"
)
func main() {
a, b := 10, 5
fmt.Printf("Add: %d + %d = %d\n", a, b, mathutils.Add(a, b))
fmt.Printf("Multiply: %d * %d = %d\n", a, b, mathutils.Multiply(a, b))
// Generate a random number between 1 and 100
randomNum := mathutils.Random(1, 100)
fmt.Printf("Random number between 1 and 100: %d\n", randomNum)
}
Conclusion
In this guide, you learned how to:
- Create a reusable Go module for mathematical operations.
- Call that module from another project.
- Link modules locally using the
replace
directive. - Extend the module with a function to generate random numbers.
By following these steps, you now have a better understanding of how Go modules work and how to structure your Go code for reusability. This knowledge will serve you well as you continue to build more complex applications.