Contents
Outline
In this blog post, I will introdue what the Module
is and how to use the module in Golang. You can see the full source code of this blog post on the link below.
Module
Before the version 1.11
of Golang, the module feature doesn’t exist. In the version 1.11
, the module feature was introduced, and the feature is completed in the version 1.13
. In the version 1.13
, the module feature is optional. In the version 1.16
, the module became the default specification for Golang.
In Golang, the module is a collection of packages, so one module can include multipe packages. this module allows Golang to manage the dependencies of packages, and the module is used as the package management system.
The module manages the package by the tree structure. The module creates the go.mod
file to define the module and manage the dependencies.
go mod init
In Golang, you can create a module by executing the command below.
go mod init MODULE_NAME
At this time, the name of the module should be unique. So, normally, people use the GitHub
repository URL or URL structure for the module name.
go mod init github.com/dev-yakuza/study-golang/module/greeting
greeting module
Next, let’s create an example to understand the module in Golang. First, let’s create the greeting
module that will be used in other modules.
mkdir greeting
cd greeting
touch main.go
To create the greeting
module, create the greeting
folder and create the main.go
file in the greeting
folder.
package greeting
import "fmt"
func Hello(name string) string {
message := fmt.Sprintf("Hello, %s", name)
return message
}
The greeting
module includes the greeting
package.
package greeting
The greeting
package has the Hello
function, and the function is created for the public function(Starts with an upper case letter).
func Hello(name string) string {
message := fmt.Sprintf("Hello, %s", name)
return message
}
The Hello
function gets the name
parameter and returns the Hello, NAME
string.
Create greeting module
Next, execute the following command to create the module with the greeting
package.
# cd greeting
go mod init github.com/dev-yakuza/study-golang/module/greeting
When you execute the command, you can see the go.mod
file is created in the folder.
module github.com/dev-yakuza/study-golang/module/greeting
go 1.17
hello module
Next, let’s create the hello
module that will be used in the greeting
module.
# cd ..
mkdir hello
cd hello
touch main.go
Create the hello
folder in the same location of the greeting
folder. And create the main.go
file in the folder and modify it like the below.
package main
import (
"fmt"
"github.com/dev-yakuza/study-golang/module/greeting"
)
func main() {
message := greeting.Hello("John")
fmt.Println(message)
}
The hello
module has the main
package and the main
function. In other words, the hello
module is the entry point of the program. This program will use the greeting
module that we’ve created above. To use the greeting
module, we need to impor the greeting
module by using the name of the module.
import (
"fmt"
"github.com/dev-yakuza/study-golang/module/greeting"
)
Next, the program will print the message by the Hello
function that is provide by the greeting
module.
func main() {
message := greeting.Hello("John")
fmt.Println(message)
}
When you write the code like this, the following error that can’t find the greeting
module will be shown.
could not import github.com/dev-yakuza/study-golang/module/greeting (cannot find package "github.com/dev-yakuza/study-golang/module/greeting" in any of
/usr/local/Cellar/go/1.17/libexec/src/github.com/dev-yakuza/study-golang/module/greeting (from $GOROOT)
/Users/jeonghean/go/src/github.com/dev-yakuza/study-golang/module/greeting (from $GOPATH))compilerBrokenImport
Next, let’s create the hello
module to use the greeting
module to fix the error.
Create hello module
Execute the command below to create the hello
module.
go mod init github.com/dev-yakuza/study-golang/module/hello
When you execute it, you can see the go.mod
file is created in the hello
folder.
module github.com/dev-yakuza/study-golang/module/hello
go 1.17
go mod tidy
In Golang, when you use the packages that are not provided by default, you need to download the package. At this time, you can use the following command to download the package in the module.
go mod tidy
However, the greeting
package that we’ve created isn’t published(we’ve not shared the code in GitHub), so the following error occurs.
go: finding module for package github.com/dev-yakuza/study-golang/module/greeting
github.com/dev-yakuza/study-golang/module/hello imports
github.com/dev-yakuza/study-golang/module/greeting: module github.com/dev-yakuza/study-golang@latest found (v0.0.0-20211026013945-559aef3c74a0), but does not contain package github.com/dev-yakuza/study-golang/module/greeting
In this case, we can upload the greeting
module to GitHub
and share the code to fix the error. or, we can modify the go.mod
file to refer the package from the local.
go mod edit -replace
Now, let’s modify the go.mod
file to refer the package from the local. Execute the command below to refer the local package.
go mod edit -replace github.com/dev-yakuza/study-golang/module/greeting=../greeting
And then, execute the command below to configure the package.
go mod tidy
Execute
Now, we’ve ready to use the module. To check it, execute the following command in the hello
folder.
# cd hello
go run main.go
When you execute the command, you can see the following result.
Hello, John
Extenal module folder
When you execute the command below in Golang, you can download the extenal modules and use them in the program.
go mod tidy
The downloaded modules are stored in the GOPATH/pkg
folder.
go env
When you execute the command above, you can find the GOPATH
. And when you go to the folder, you can see the pkg
folder.
...
GOOS="darwin"
GOPATH="/Users/dev-yakuza/go"
GOPRIVATE=""
...
Completed
Done! we’ve seen what the module is and how to use the module. From now, you can use the extenal packages and share the packages for the other people.
Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!
App promotion
Deku
.Deku
created the applications with Flutter.If you have interested, please try to download them for free.