Golang multi-module development
Today, learn the basics of multi-modules in Golang, learn the operating principles of multi-modules, and use multi-modules to allow developers to build and run code in other multiple modules. Improve code reuse, thereby improving development efficiency.
In today's study, two modules will be created in the working workspace, then references between modules will be made, and the program results will be run.
prerequisite
basic requirements
- Go 1.18 and later
- Appropriate compilation tool - text editor also suffices
- Command Terminal - Linux, Mac system shell, Windows system Cmd, PowerShell
local environment
- Go version information
- System Information - MacOS
- Editing Tools - Sublime Text
code development
create module
-
Open the shell terminal and enter the code directory
-
Create a workspace and enter
$ mkdir workspace $ cd workspace
-
Initialize module - create a new hello module, which depends on the golang.org/x/example module
- Create the hello module
$ mkdir hello $ cd hello $ go mod init example.com/hello go: creating new go.mod: module example.com/hello
- add dependencies
$ go get golang.org/x/example
-
In the hello directory, create a hello.go source file with the following content
package main import ( "fmt" "golang.org/x/example/stringutil" ) func main() { fmt.Println(stringutil.Reverse("Hello")) }
-
run the program
$ go run example.com/hello
create workspace
In this step, create go.workwe
-
Initialize the workspace - Execute the following command in the workspace directory, which will automatically generate a go.work file
$ go work init ./hello
-
The content of the go.work file is as follows
The syntax of go.work file content is similar to go.mod. The first line tells the Go compiler which version to use to interpret the file; the second line tells Go, the main module when compiling;
-
Run the program - execute the command in the workspace directory (execute in the hello directory before the difference)
$ go run example.com/hello
The Go command treats all modules in the workspace as master modules. This allows us to reference packages inside modules, and even packages outside modules. Running the go run command outside of a module or workspace will result in an error because the go command doesn't know which modules to use.
Next, we'll add a local copy of the golang.org/x/example module to the workspace. Then, we'll add a new function to the stringtil package that we can use in place of Reverse.
Modify the source code
In this step, we will download a copy of the Git repo containing the golang.org/x/example module, add it to the workspace, and add a new function to it, which we will then use from the hello program.
-
Clone the code - execute the following command in the workspace directory to copy the git code
$ git clone https://go.googlesource.com/example Cloning into 'example'... remote: Total 165 (delta 27), reused 165 (delta 27) Receiving objects: 100% (165/165), 434.18 KiB | 1022.00 KiB/s, done. Resolving deltas: 100% (27/27), done.
Unfortunately, the domestic environment may not be able to copy the code through the git clone command, and the following errors will occur
You can click this link official website Download the code tarball
After the download is complete, decompress the compressed package and rename it to example. The code structure of the workspace is as follows
-
Add Module - add a module using the command
go work use ./example
So far, the go.work file depends on two modules
-
New function - Add a toupper.go file in the workspace/example/stringutil directory, the file content is as follows
package stringutil import "unicode" // ToUpper uppercases all the runes in its argument string. func ToUpper(s string) string { r := []rune(s) for i := range r { r[i] = unicode.ToUpper(r[i]) } return string(r) }
-
Call the new function - modify the workspace/hello/hello.go code file
func main() { fmt.Println(stringutil.ToUpper("Hello")) fmt.Println(stringutil.Reverse("Hello")) }
-
Execute code - execute in the workspace directory
go run example.com/hello
So far, we have learned to pull the original code on github, and learned how to add functions and how to call them.