英文:
golang offline install third party package
问题
我正在尝试在离线环境中安装Go语言的第三方包。我想在离线环境中像在线环境一样使用"go get <第三方包>"的方式。我可以从GitHub获取第三方包的源代码并将其放入离线环境,但是在Go语言中无法将其作为包使用。
我应该怎么做?
英文:
I'm trying to install third party package of golang in offline.
I want to do something like go get <third party package> 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?
答案1
得分: 1
根据@emptyhua的评论,这个链接对我有用:https://gist.github.com/gmolveau/f09c1038ca622620e54d0579ba06ea96#file-golang_offline-md
参考步骤如下:
1)在在线机器上,确保当前目录中存在go.mod和go.sum文件,在该目录下创建一个go文件,内容如下:
import (
_ "github.com/gorilla/mux"
_ "github.com/sirupsen/logrus"
)
func main() {}
2)运行命令:go mod vendor
3)将go.mod、go.sum和vendor目录复制到离线机器上
4)运行命令:go run -mod=vendor main.go
为了测试离线功能,我使用了一个没有网络连接的容器:
podman run --rm -it --net none -v $(pwd):/go/src:z golang
go run -mod=vendor main.go
希望对你有帮助!感谢@gmolveau提供的离线go模块。
英文:
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
答案2
得分: 0
你可以尝试使用vendor/
目录。
请参考我在https://stackoverflow.com/a/76688264/490291上的回答。
如果该软件包是Debian的一部分,也许以下操作就足够了:
ln -s /usr/share/gocode/src vendor
软件包github.com/$USER/$REPO
将在vendor/github.com/$USER/$REPO/
中搜索,这样你甚至可以使用git submodule
来检出该软件包:
git submodule add https://github.com/$USER/$REPO.git vendor/github.com/$USER/$REPO/
> 据我所知(但我从未尝试过),git submodule update --recursive
在这里的效果不如预期。因此,如果某个子模块本身有一个vendor/
目录,你必须将其与你自己的vendor/
目录混合在一起,因为该功能不会递归处理。至少这是我从关于该功能的(过于稀缺)文档中得到的印象。
>
> 当然,你可以使用软链接来映射子路径,就像这样:
> bash > yes n | for a in vendor/*/*/*/vendor/*; do [ -d "$a" ] && ln -is --relative "$a"/* "vendor/`basename "$a"`/"; done >
还有一个不太为人知的git
功能,允许你将任何URL映射到任何内容。因此,例如,如果你做了一个镜像(可以使用配置工具或rsync
将其复制到你的服务器上):
git clone --mirror https://github.com/$USER/$REPO.git /srv/git/github.com/$USER/$REPO.git
你可以使用一行命令将https://github.com/$USER/$REPO.git
映射到/srv/git/github.com/$USER/$REPO.git
:
git config --global url./srv/git/github.com/.insteadOf https://github.com/
> 所以:
>
> - 在某台远程网络连接的计算机上进行所有的--mirror
克隆操作
> - 将镜像的仓库复制到离线计算机的/srv/git/
目录下
> - 然后执行git config --global url./srv/git/.insteadOf https://
> - 然后将git submodule add
添加到vendor/
中
>
> 或者使用git clone --recursive /srv/git/yoursource.git
,该命令已经在适当位置放置了.gitmodules
文件,其中包含了https://github.com/
的URL,这些URL在离线环境之外有效,但在离线环境中也有效,因为它们被重新映射到/srv/git/
请注意,你可以使用这种方式映射所有的http://
和https://
URL,例如https://$SERVER/$USER/$REPO.git
可以映射为/srv/git/$SERVER/$USER/$REPO.git
,使用以下命令:
git config --global url./srv/git/.insteadOf https://
git config --global url./srv/git/.insteadOf http://
你需要记住的唯一一件事是:不要省略检出/子模块URL中的.git
后缀,也不要添加尾部的/
。
这通常会使事情变得更加简单。因为通常你希望将“裸”git
仓库(--mirror
)命名为something.git
而不是something
,因为通常something/
应该是工作树的检出。由于git
在大多数情况下都足够智能,所有的git
服务如GitHub或GitLab也是如此,添加.git
后缀只有在重新映射时才会有好处,因为你可以将something.git
和something-wiki.git
映射为不同的内容(不带扩展名的something
包括something-wiki
)。
git
功能url.*.insteadOf
只映射前缀,你不能像更改前缀那样轻松地更改后缀。因此,如果你需要添加后缀,你必须单独重新映射每个URL,这会让事情变得不方便。此外,我认为以.git
结尾的git
仓库URL看起来更自然。你的情况可能有所不同。
英文:
You can try the vendor/
directory.
See my answer at https://stackoverflow.com/a/76688264/490291
If the package is part of Debian, perhaps even following is enough:
ln -s /usr/share/gocode/src vendor
A package github.com/$USER/$REPO
is searched at vendor/github.com/$USER/$REPO/
, such that you can even use git submodule
for checking out the package:
git submodule add https://github.com/$USER/$REPO.git vendor/github.com/$USER/$REPO/
> AFAICS (but I never tried it) git submodule update --recursive
does not work here as expected. So if some submodule has a vendor/
directory itself, you must mix this into your own vendor/
directory, as this feature does not do recursion. At least that is the impression I got from the (too scarce) documentation about that feature.
>
> You can use softlinks to map the sub-paths, of course, like with
> bash
> yes n | for a in vendor/*/*/*/vendor/*; do [ -d "$a" ] && ln -is --relative "$a"/* "vendor/`basename "$a"`/"; done
>
There is a less known git
feature which allows you to map any URL to anything. So for example if you did a mirror (something you can copy to your servers with your provisioning tool or rsync
):
git clone --mirror https://github.com/$USER/$REPO.git /srv/git/github.com/$USER/$REPO.git
you can map https://github.com/$USER/$REPO.git
to /srv/git/github.com/$USER/$REPO.git
with a single line:
git config --global url./srv/git/github.com/.insteadOf https://github.com/
> So:
>
> - On some remote networked PC do all the --mirror
cloning
> - Copy the mirrored repositories to /srv/git/
on the offlined PC
> - Then do git config --global url./srv/git/.insteadOf https://
> - Then do the git submodule add
into vendor/
>
> Or git clone --recursive /srv/git/yoursource.git
which already has the .gitmodules
file in place, which carries https://github.com/
-URLs which only work outside of the offline environment, but work offline, too, as they are remapped to /srv/git/
Note that you can map all http://
and https://
URLs this way, such that https://$SERVER/$USER/$REPO.git
becomes /srv/git/$SERVER/$USER/$REPO.git
with
git config --global url./srv/git/.insteadOf https://
git config --global url./srv/git/.insteadOf http://
The only thing you need to remember is: Do not leave the suffix .git
away in your checkouts/submodule URLs. And do not add a trailing /
.
This usually makes live much easier. Because you usually want to name "bare" git
repositories (--mirror
) something.git
and not something
, as usually something/
should be the checkout of a worktree. As git
is intelligent enough to remove the .git
in most cases, and all git
services like GitHub or GitLab do this, too, adding the .git
only adds benefits when remapping, because you can map something.git
and something-wiki.git
differently (without extension something
includes something-wiki
).
The git
feature url.*.insteadOf
only maps the prefix, you cannot change the suffix as easily as well. So if you happen to need to add a suffix, you must remap each URL individually, which makes things uncomfortable. Also I consider ending git
repo URLs on .git
to look much more natural. YMMV.
答案3
得分: -2
首先,你需要为工作目录设置GOPATH
。
然后,你可以使用以下离线包,只需为包的路径创建一个mod
文件。
使用以下代码初始化go mod文件:
go mod init <输入你的GitHub个人资料链接>
例如:
go mod init github.com/amshashankk
之后,当你导入包时,它们将在代码的导入部分中写成以下形式:
github.com/amshashankk/package
。
英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论