golang offline install third party package - go

I'm trying to install third party package of golang in offline.
I want to do something like go get in an online environment in an offline environment. I can get the source of the third party package from github and put it in an offline environment, but then I can't use it as a package in golang.
What should i do?

As #emptyhua commented, this link worked for me: https://gist.github.com/gmolveau/f09c1038ca622620e54d0579ba06ea96#file-golang_offline-md
For reference here:
On the online machine ensure go.mod and go.sum exist in current dir
create a go file with:
import (
_ "github.com/gorilla/mux"
_ "github.com/sirupsen/logrus"
)
func main() {}
run: go mod vendor
Copy go.mod, go.sum and vendor directory to the offline machine
run: go run -mod=vendor main.go
To test offline, I used a container with no network:
podman run --rm -it --net none -v $(pwd):/go/src:z golang
cd src
go run -mod=vendor main.go
Hope that helps! Thanks #gmolveau - offline go mods

First of all, you have to set the GOPATH for the working directory.
After then you can use the following offline package just you have to create a mod file for the path of the packages.
Initialize go mod file using below code
go mod init <Enter your GitHub profile link>
For example:
go mod init github.com/amshashankk
after this when you import the packages they will be written as following in the import section of the code
github.com/amshashankk/package.

Related

go: module found but does not contain package

I am trying to install the net package for go but get "does not contain package error".
Terminal screenshot:
I have consulted: go module #latest found but does not contain package but none of the solutions seem to work for me.
I am using go version go1.18.5 linux/amd64
You have to initialize your module with go mod init in the project root directory
For local codebase
go mod init test
OR for hosted codebase e.g. github repo: test, github user: radiant
go mod init github.com/radiant/test
It will produce a go.mod file.
Then you can get the required package as:
go get golang.org/x/net
go mod tidy
Then import and use the net packages.
Hope this helps.
go get -u golang.org/x/net
go install used for executable packages

cannot find package google.golang.org/grpc

When I try to import the grpc package I get the following error:
could not import google.golang.org/grpc (cannot find package "google.golang.org/grpc" in any of
/usr/local/go/src/google.golang.org/grpc (from $GOROOT)
/home/ansh/Go/src/google.golang.org/grpc (from $GOPATH))
This is my gopath (incase it helps):
export GOPATH="$HOME/Go"
export PATH="$PATH:/usr/local/go/bin:$GOPATH/bin"
I did install these two packages:
$ go install google.golang.org/protobuf/cmd/protoc-gen-go#v1.26
$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc#v1.1
But it doesnt seem to work
In case you are using go modules, there is no need to set GOPATH anymore. However if GOPATH is set and you want to use a package from your project directory you need to set GO111MODULE=off, because by default GO111MODULE is set as ON. So even if the package google.golang.org/grpc is in your GOPATH you will have to force Go not to use the Go modules.
So something like this should work in case you are still opting of using GOPATH.
$ GO111MODULE=off && go install google.golang.org/protobuf/cmd/protoc-gen-go#v1.26
$ GO111MODULE=off && go install google.golang.org/grpc/cmd/protoc-gen-go-grpc#v1.1
If you haven't defined a go.mod file yet, please run the "go mod init ..." command to create it.
Then run the following command:
go get -u google.golang.org/grpc
in case grpc with updated gopath, dependencies will be looking gopath
Step1 : create go mod file in base folder of the project
"go mod init"
Step2 : run
"go mod tidy"
which loads the dependencies to "go.sum"

When I using `go install`, it returns not a main package

I used import "github.com/go-redis/redis/v8" in my code. The environment is go1.17.2 Windows AMD64. I executed go install github.com/go-redis/redis/v8#latest, but the result is package github.com/go-redis/redis/v8 is not a main package. What's wrong of my operations or the config of environment. go env GO111MODULE=on.
And when I execute go run main.go, it shows cannot find package at the line of import github.com/go-redis/redis/v8.
Content in go.mod, (with simple go mod init & go mod tidy):
module ...
go 1.17
require github.com/go-redis/redis/v8 v8.11.4
require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
)
The module name of your go.mod is invalid. I try the similar module name in my environment and compile with go build, it reports:
$ go build
go: malformed module path "...": invalid path element "..."
Try a name like:
module tempredis
go 1.17
require github.com/go-redis/redis/v8 v8.11.4
Or create the module with command go mod init tempredis then add a dependency of github.com/go-redis/redis/v8.
Refer to document.
I use import "github.com/go-redis/redis", and restart the process (include go mod init, go mod tidy, go install), it shows right result finally. But the version of go redis changes to v6.15.9+incompatible in go.mod file automatedly.

go 1.14 to 1.17 update -> modules not working anymore

After updating Go from 1.14 to 1.17 I'm getting this error:
main.go:10:2: no required module provides package github.com/gin-gonic/gin: \
go.mod file not found in current directory or any parent directory; \
see 'go help modules'
I used to be able to fix that with go get github.com/gin-gonic/gin but now that doesn't help. Did something change?
I can repro this if I have a file like this:
package hello
import _ "github.com/gin-gonic/gin"
and run these commands:
go mod init hello
go build
Fix for me is running this command:
go mod tidy
As of Go 1.16, “Module-aware mode is enabled by default, regardless of whether a go.mod file is present in the current working directory or a parent directory. More precisely, the GO111MODULE environment variable now defaults to on.”
See the Migrating to Go Modules blog post for a quick overview of how to migrate, or Tutorial: Create a Go Module for more detail.
According to documentation we must use go install example.com/cmd#latest instead of go get for go 1.17+

Why cannot import library from go get?

I downloaded go-redis client using
go mod init github.com/my/repo
go get github.com/go-redis/redis/v8
But it showed cannot find package "go.opentelemetry.io/otel/api/trace". So I deleted go-redis from ${GOPATH}/src/github.com and then tried running it again
go get github.com/go-redis/redis/v8
But it does nothing. Doesn't show any error or any message. But when I try to import package it says
cannot find package "github.com/go-redis/redis/v8" in any of:
 /usr/lib/go/src/github.com/go-redis/redis/v8 (from $GOROOT)
 /home/username/go/src/github.com/go-redis/redis/v8 (from $GOPATH)
I tried go mod tidy go mod clean but none worked. What should I do?
ls $GOPATH , if is not showing your path of golang library source, you should set path first,
If point 1 is able, you should just doing :go mod tidy
tidy argument make you doing download package with sync method, without doing go get separately.
I would rather use go vendoring, it will add your dependencies to vendor/ and -mod=vendor will tell golang to search for dependencies locally.
Remove go.sum
Type export GOFLAGS=-mod=vendor
Type go mod tidy && go mod vendor
If you can't export variables, use go run and go build with the prefix GOFLAGS=-mod=vendor, for example GOFLAGS=-mod=vendor go run cmd/main/main.go
Don't forget to add vendor/ to your .gitignore

Resources