Home > Blockchain >  Copy Go package from Github to offline VM
Copy Go package from Github to offline VM

Time:12-16

I am trying to build a Go program in a offline VM. The program only has one dependency which is https://github.com/go-log/log.

When trying to build the program it says.

go: download github.com/go-log/log v0.2.0

However since I do not have internet it won't work. I have tried downloading the code from Github and placing it in the directories that I thought would be correct.

c:\Program Files\Go\pkg\mod\github.com\go-log\[email protected]
and 
c:\users\user\go\pkg\mod\github.com\go-log\[email protected]

When doing this it still attempts to download from the internet. I am not sure what I need to do. Do I need to compile the log program?

CodePudding user response:

You can use Vendoring in golang.

go mod vendor also creates the file vendor/modules.txt that contains a list of vendored packages and the module versions they were copied from. When vendoring is enabled, this manifest is used as a source of module version information, as reported by go list -m and go version -m. When the go command reads vendor/modules.txt, it checks that the module versions are consistent with go.mod. If go.mod has changed since vendor/modules.txt was generated, the go command will report an error. go mod vendor should be run again to update the vendor directory.

  1. In an online machine use go mod download and go mod vendor in the folder containing your go.mod. This will download and place your requirements in a folder named vendor in the same directory.

  2. Move this folder to your offline machine behind your go.mod file.

  3. use go build -mod=vendor to build your project. this way go compiler will use dependencies stored in the vendor folder.

CodePudding user response:

You probably want go mod vendor. Emphasis mine:

The go mod vendor command constructs a directory named vendor in the main module’s root directory that contains copies of all packages needed to support builds and tests of packages in the main module. Packages that are only imported by tests of packages outside the main module are not included. As with go mod tidy and other module commands, build constraints except for ignore are not considered when constructing the vendor directory.

When vendoring is enabled, the go command will load packages from the vendor directory instead of downloading modules from their sources into the module cache and using packages those downloaded copies. See Vendoring for more information.

See also: what is the purpose of `go mod vendor` command?

Given a simple main.go that imports github.com/go-log/log/print:

package main

import (
    "github.com/go-log/log/print"
)

type MyLogger struct{}

func (l MyLogger) Print(v ...interface{}) {}

func (l MyLogger) Printf(format string, v ...interface{}) {}

func main() {
    printer := print.New(MyLogger{})
    printer.Log("Blah")
}

... running go mod vendor will vendor those files and you will end up with the following in your vendor folder:

|-github.com
| |-go-log
| | |-log
| | | |-LICENSE
| | | |-print
| | | | |-print.go
|-modules.txt

The next time you go build, the version of print from the vendor folder will be used instead.

  •  Tags:  
  • go
  • Related