go mod tidy<\/strong><\/li><\/ul>\n\n\n\nBefore looking at each of the ways, again let’s create a module first<\/p>\n\n\n\n
go mod init sample.com\/learn<\/code><\/pre>\n\n\n\n<\/span>Directly adding it to the go.mod file<\/strong><\/span><\/h2>\n\n\n\nWe can add direct dependency to the go.mod file too. Let’s do that<\/p>\n\n\n\n
Add below dependency to the go.mod<\/strong> file<\/p>\n\n\n\nrequire github.com\/pborman\/uuid v1.2.1<\/code><\/pre>\n\n\n\nWith this dependency go.mod file will look like below<\/p>\n\n\n\n
module sample.com\/learn\n\ngo 1.14\n\nrequire github.com\/pborman\/uuid v1.2.1<\/code><\/pre>\n\n\n\nNow we need to download the newly added dependency as well. Fo that we can use the below command<\/p>\n\n\n\n
go mod download<\/code><\/pre>\n\n\n\nThis command will download the github.com\/pborman\/uuid<\/strong> module as well all its dependencies. Also, it will update the go.sum<\/strong> file with the checksum and version of all direct and indirect dependencies. go build as well as go install also will download the dependencies and also build the binary. go run will also download and run the binary as well. go mod download command is used when you want to pre-download the dependencies without build or running it.<\/p>\n\n\n\n<\/span>Do a go get<\/strong><\/span><\/h2>\n\n\n\nSimply doing a go get will also the add the dependency in the go.mod file. Remove the uuid dependency we added above from go.mod file and clean up go.sum file. Now run below command<\/p>\n\n\n\n
export GO111MODULE=on\ngo get github.com\/pborman\/uuid<\/code><\/pre>\n\n\n\nNow check the contents of go.mod file. Do a cat go.mod<\/strong><\/p>\n\n\n\nmodule sample.com\/learn\n\ngo 1.14\n\nrequire github.com\/pborman\/uuid v1.2.1 \/\/indirect<\/code><\/pre>\n\n\n\nThe dependency will be marked as \/\/indirect<\/strong> as it is not being used in any of the source files. Once you do a go build after using this in the source files, the \/\/indirect will be removed automatically by go. Also it will update the go.sum<\/strong> file with the checksum and version of all direct and indirect dependencies.<\/p>\n\n\n\n<\/span>Add the dependency to your source code and do a go mod tidy<\/strong><\/span><\/h2>\n\n\n\nBasically go mod tidy command makes sure that your go.mod files reflects the dependencies that you have actually used in your project. When we run go mod tidy command then it will do two things<\/p>\n\n\n\n
- Add any dependency which is imported in the source files<\/li><\/ul>\n\n\n\n
- Remove any dependency which is mentioned in the go.mod<\/strong> file but not imported in any of the source files.<\/li><\/ul>\n\n\n\n
Let’s see an example. Create a module with an import path as “sample.com\/learn<\/strong>“<\/p>\n\n\n\ngo mod init sample.com\/learn<\/code><\/pre>\n\n\n\nLet’s create a file named uuid.go <\/strong>in the same directory with below contents<\/p>\n\n\n\nuuid.go<\/strong><\/p>\n\n\n\npackage main\n\nimport (\n\t\"fmt\"\n\t\"strings\"\n\n\t\"github.com\/pborman\/uuid\"\n)\n\nfunc main() {\n\tuuidWithHyphen := uuid.NewRandom()\n\tuuid := strings.Replace(uuidWithHyphen.String(), \"-\", \"\", -1)\n\tfmt.Println(uuid)\n}<\/code><\/pre>\n\n\n\nNotice that we have imported the dependency in the uuid.go as well<\/p>\n\n\n\n
\"github.com\/pborman\/uuid\"<\/code><\/pre>\n\n\n\nLet’s run the below command<\/p>\n\n\n\n
go mod tidy<\/code><\/pre>\n\n\n\nThis command will download all the dependencies that are required in your source files and update go.mod<\/strong> file with that dependency. After running this command let’s now let’s again examine the contents of go.mod<\/strong> file. Do a cat go.mod<\/strong><\/p>\n\n\n\nmodule sample.com\/learn\n\ngo 1.14\n\nrequire github.com\/pborman\/uuid v1.2.1<\/code><\/pre>\n\n\n\n<\/span>Adding a vendor directory<\/strong><\/span><\/h1>\n\n\n\nIf you want to vendor your dependencies, then below command can be used to achieve the same<\/p>\n\n\n\n
go mod vendor<\/code><\/pre>\n\n\n\nIt will create a vendor directory inside your project directory. You can also check in the vendor directory to your VCS (Version Control System). This becomes useful in sense that none of the dependency needs to be downloaded at run time as it is already present in the vendor folder checked into VCS<\/p>\n\n\n\n
<\/span>Module Import Path<\/strong><\/span><\/h1>\n\n\n\nWe have already seen that module import path is the prefix path that is used to import all packages within that module<\/p>\n\n\n\n
There can be three cases that decide what import path name can be used with modules.<\/p>\n\n\n\n
- The module is a utility module and you plan to publish your module<\/li><\/ul>\n\n\n\n
- The module is a utility module and you don’t plan to publish your module<\/li><\/ul>\n\n\n\n
- The module is a executable module<\/li><\/ul>\n\n\n\n
The module is a utility module and you plan to publish your module<\/strong><\/p>\n\n\n\nIf you plan to publish your module then the module name should match the URL of the repo which host that module. Go tries to download dependencies from the VCS using the same import path of the module.<\/p>\n\n\n\n
The module is a utility module and you don’t plan to publish your module<\/strong><\/p>\n\n\n\nThis is the case when you only mean to use the utility module locally only. In this case the import path can be anything.<\/p>\n\n\n\n
The module is a executable module<\/strong><\/p>\n\n\n\nIn this case also module import path can be anything. The module import path can be a non-url even if you plan to commit your module into VCS as it will not be used by any other module<\/p>\n\n\n\n
However it is a good practice to use meaningful import path while creating module<\/p>\n\n\n\n
<\/span>Importing package within same module<\/strong><\/span><\/h1>\n\n\n\nAny package within the same module can be imported using the import path of module + directory containing that package. To illustrate lets create a module<\/p>\n\n\n\n
- Make a learn<\/strong> directory<\/li><\/ul>\n\n\n\n
- Create a module with import path as “sample.com\/learn”<\/strong><\/li><\/ul>\n\n\n\n
go mod init sample.com\/learn<\/code><\/pre>\n\n\n\n- Now create main.go (Having main package and main function)<\/li><\/ul>\n\n\n\n
- And math\/math.go – math package<\/li><\/ul>\n\n\n\n
main.go<\/strong><\/p>\n\n\n\npackage main\n\nimport (\n\t\"fmt\"\n\t\"sample.com\/learn\/math\"\n)\n\nfunc main() {\n\tfmt.Println(math.Add(1, 2))\n}<\/code><\/pre>\n\n\n\nmath\/math.go<\/strong><\/p>\n\n\n\npackage math\n\nfunc Add(a, b int) int {\n return a + b\n}<\/code><\/pre>\n\n\n\nSee how we have imported the math package in the main.go file<\/p>\n\n\n\n
\"sample.comlearn\/math\"<\/code><\/pre>\n\n\n\nHere the import path is import path of module which is sample.com\/learn<\/strong> + directory containing the package which is math. <\/strong>Hence “sample.com\/learn\/math” <\/strong>. Packages in nested directory can also be imported in the same way. The way it works is that since the prefix is the module import path, hence go will know that you are trying to import from the same module. So it will directly refer it instead of downloading it.<\/p>\n\n\n\n<\/span>Importing package from different module locally<\/strong><\/span><\/h1>\n\n\n\nThere are cases when we want to import a module which is present locally. Let’s understand how we can import such a module. But first, we have to create a module that can be used by others and then import it into the other module. For that let’s create two modules<\/p>\n\n\n\n
- sample.com\/math<\/strong> module<\/li>
- school<\/strong> module<\/li><\/ul>\n\n\n\n
school<\/strong> module will be calling code of the sample.com\/math<\/strong> module<\/p>\n\n\n\nLet’s first create the sample.com\/math<\/strong> module which will be used by school<\/strong> module<\/p>\n\n\n\n- Make a math<\/strong> directory<\/li><\/ul>\n\n\n\n
- Create a module with import path as sample.com\/math<\/strong><\/li><\/ul>\n\n\n\n
go mod init sample.com\/math<\/code><\/pre>\n\n\n\n- Create a file math.go<\/strong> with below contents in the math<\/strong> directory<\/li><\/ul>\n\n\n\n
package math\n\nfunc Add(a, b int) int {\n\treturn a + b\n}<\/code><\/pre>\n\n\n\nNow <\/strong>let’s create the school module<\/p>\n\n\n\n- Now <\/strong>create a school<\/strong> directory in the same path as math<\/strong> directory side by side<\/li><\/ul>\n\n\n\n
- Create a module name school<\/strong><\/li><\/ul>\n\n\n\n
go mod init school<\/code><\/pre>\n\n\n\n