How do local modules work in Go?

Asked

Viewed 65 times

1

I saw in this question that it is possible to define local modules in Go using the GOPATH. However, in the solution of this question, the file is not used go.mod What is this file for? There is a way to import defined packages into a folder without adding the source code to GOPATH?

2 answers

2


To create a module, just use:

go mod init github.com/seunome/seumodulo

This will create the go.mod. I suppose I make a push of that package against the github.com/seunome/seumodulo, you can take this package from there in the future.

Note: you can have several packages within a single module, so when creating new modules you should have a good reason to do so. You don’t need to create a module for each package. Personally, I only create modules when I intend to share it, or reuse in other projects.


So, let’s assume you have the meuprojeto who wants to use the seumodulo: first the meuprojeto should also be a module, ie also use:

go mod init github.com/seunome/meuprojeto

To consume the "meumodulo" created earlier, you can simply use:

go get github.com/seunome/seumodulo

This command must be executed inside the folder go.mod, if it won’t go wrong. That way, the seumodulo will be available for use within the meuprojeto.


If your package is in another folder, locally stored, you can use the replace in the go.mod:

replace github.com/seunome/seumodulo => ../caminho/para/o/seumodulo

This way, instead of getting and using the information from the Github repository, you will use that of the specified folder. This is useful if you are working on a/Fork branch for seumodulo.


If i repository is private, you can use something like:

set GOPROXY=https://proxy.golang.org,direct
set GOPRIVATE=github.com/seunome/*

That way, you can give the go get in a private repository.

1

The question solution is correct and works even in the latest versions of Go. However, since version 1.11 it is possible to use a module manager that makes it possible to define packages within a particular folder, without resorting to GOPATH.

Let’s say, for example, that you want to create a module called mymath to define custom mathematical operations. To do this, create a new directory with the module name, and navigate to it:

mkdir mymath
cd mymath

To start the module use:

go mod init mymath

This command will create a file called go.mod.

Now you can start creating the packages. As an example, let’s define power as a package for operations involving exponents. To do this we create files called operations.go (that name is not important) inside a subdirectory called power:

package power

func Power2(number int) int {
    return number*number
}

To test that our package is working, just create a file (say, main.go) at the root of the directory mymath:

package main

import (
    "fmt"
    "mymath/power"
)

func main() {
    fmt.Println(power.Power2(9))
}

To confirm that the import works run:

go run main.go

81

Browser other questions tagged

You are not signed in. Login or sign up in order to post.