我该如何在本地服务器上使用仓库进行工作 [英] How can I make go get work with a repo on a local server

查看:64
本文介绍了我该如何在本地服务器上使用仓库进行工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地服务器上有一个git repo.我可以使用 git clone user@10.xxx.yyy.zzz:/srv/git/liqid.git 从中克隆它,其中用户具有ssh访问权限,并且可以读/写到git/目录./p>

当我尝试将其与一起使用时,获得-v user@10.xxx.yyy.zzz:/srv/git/liqid.git 即可

go:不能在GOPATH模式下使用path @ version语法

我尝试了其他各种组合,例如省略了:,但是它们都失败了.

可以获取可以与本地服务器上的存储库一起使用吗?

解决方案

这是我学到的关于git repo在私有服务器上时对包和模块使用 go get 的知识.我希望它可以帮助其他人将所有步骤记录在一个地方.

在私有服务器上使用带有git存储库的软件包和模块

这是在私有服务器上使用带有git存储库的Go软件包和模块所需的所有步骤.这些是具有IP地址(例如10.xxx.yyy.zzz或192.168.xxx.yyy)的服务器.这些服务器上不存在github或gitlab,因此端口80上没有运行Web服务器,这是 go get 所基于的假设.您的专用服务器只需要运行 sshd .

安装最新版本的Go,并确保已设置GOPATH.这些示例中的代码将下载到GOPATH中的第一个元素.

您将需要网络上的另一台计算机来创建git存储库.这将是您的私人git服务器.

如果要跳过所有设置步骤和示例代码,可以检查两个关键差异部分,其中列出了在私有服务器而不是公共git存储库中使用软件包或模块时的差异.

源代码

在下面显示的用于包和模块的目录结构中,

dateutil.go stringutil.go 放入

.

dateutil.go:

  package datepackage导入时间"func GetTime()time.Time {返回时间.Now().Local()} 

stringutil.go:

 程序包字符串程序包导入字符串"func ToUpperCase(s string)string {返回字符串.} 

main.go(将main.go放在下面显示的目录结构之外的某个位置):

 程序包主要进口 ("fmt"" github.com/your-github-username/go-package-test-dateutil/datepackage"" github.com/your-github-username/go-package-test-stringutil/stringpackage")func main(){fmt.Println("github:" + stringpackage.ToUpperCase("test")))fmt.Println(" github:" + datepackage.GetTime().String())} 

这些文件可以在github.com上找到

  git@github.com:dwschulze/go-package-test-dateutil.gitgit@github.com:dwschulze/go-package-test-stringutil.gitgit@github.com:dwschulze/go-module-package-test-drivers.git 

使用GOPATH约定创建软件包

在您的GOPATH外部创建一个这样的目录结构,并添加上面的文件.这遵循GOPATH约定,但您的GOPATH中不需要这些文件.

 包├──github的│├──dateutil││└──src││└──日期包││└──dateutil.go│──└──stringutil│└──src│└──字符串包│└──stringutil.go└──your-local-git-repo-hostname├──dateutil│└──src│└──日期包│└──dateutil.go└──stringutil└──src└──字符串包└──stringutil.go 

your-local-git-repo-hostname 是将在其中创建git repos的私有git服务器的主机名或IP地址(而不是当前拥有此代码的机器).有一个未公开的要求,即 go get 命令的主机名中必须包含..如果您的私人git服务器的主机名中没有.,请使用其IP地址.使用 ssh-copy-id 向您的私有git服务器添加ssh密钥访问.

在github上使用私人仓库

我们将从最简单的情况开始,使用来自github.com的软件包.您将需要一个具有ssh密钥访问设置的github帐户.

在上面使用 git init

创建的 github/下的两个 src/目录中创建git repos

 包├──github的│├──dateutil││└──src││└──...│──└──stringutil│└──src|└──... 

分别将 datepackage/ stringpackage/目录添加并提交到您的git存储库中.

在github.com上的帐户中创建两个名为 go-package-test-dateutil go-package-test-stringutil 的私有github仓库.请按照说明在您的 src/目录中的本地git仓库中将遥控器设置为它们各自的github仓库.推送代码.

由于您的存储库是私有的,因此必须使用ssh公共密钥访问权限来下载代码.由于默认情况下 go get 使用https,因此您需要将其添加到〜/.gitconfig 文件中:

  git config --global url."git@github.com:".insteadOf"https://github.com/" 

运行以下命令,将您刚刚推送到github.com的代码放入 GOPATH :

 开始获取github.com/your-github-username/go-package-test-stringutil/stringpackage/去获取github.com/your-github-username/go-package-test-dateutil/datepackage/ 

这些软件包已下载到GOPATH中第一个元素的 pkg/ src/目录中.

在上面创建的带有 main.go 文件的目录中,键入 go run main.go ,结果将打印到控制台.

在专用服务器上创建Git存储库

现在,您将在私人git服务器上创建git repos.为了简单起见,您只需要使用 git init --bare .无需安装github或gitlab.在您的私人git服务器上,您需要运行 sshd 并从您的代码所在的计算机上具有ssh密钥访问权限.新的存储库将位于/home/myusername/gitrepo 中.

  git init/home/myusername/gitrepo/go-package-test-dateutil --baregit init/home/myusername/gitrepo/go-package-test-stringutil --bare 

go get 命令有一个未记录的要求,即主机名中必须包含..如果私有git服务器的主机名中没有.,则使用其IP地址,这就是本示例其余部分的内容.假设机器的IP地址为192.168.0.12

将先前创建的 dateutil.go stringutil.go 文件复制到 your-local-git-repo-hostname :

 包├──github的|...└──your-local-git-repo-hostname├──dateutil│└──src│└──日期包│└──dateutil.go└──stringutil└──src└──字符串包└──stringutil.go 

像以前一样在两个 src/目录中创建本地git repos,然后添加并提交代码.将遥控器设置为您在私人git服务器上创建的git repo

  git remote add origin myusername @ your-local-git-repo-hostname:gitrepo/go-package-test-dateutilgit remote add origin myusername @ your-local-git-repo-hostname:gitrepo/go-package-test-stringutil 

您需要在〜/.gitconfig 中为您的私人git服务器添加另一个条目:

  git config --global url."myusername @ your-local-git-repo-hostname:". 

现在输入密码.请注意,该代码尚未在您的GOPATH中.

使用 go get 从您的私人git服务器检索代码.有必要添加一个".git"在私有git服务器上使用 git init --bare 创建git仓库的目录名称的后缀.这就告诉 go ,这是一个git存储库,而不是其他版本控制系统.

 去获取192.168.0.12/gitrepo/go-package-test-stringutil.git/stringpackage去获取192.168.0.12/gitrepo/go-package-test-dateutil.git/datepackage 

这些软件包已下载到 GOPATH 中第一个元素的pkg/和src/目录中.

 └──src├──192.168.0.12│└──gitrepo│├──go-module-test-dateutil.git││├──dateutil.go││└──go.mod│├──go-package-test-dateutil.git││└──日期包││└──dateutil.go│└──go-package-test-stringutil.git│└──字符串包│└──stringutil.go 

在您的 main.go 代码中,将两个import语句添加到私有git服务器上的包中

 程序包主要进口 (dpkg"192.168.0.12/gitrepo/go-package-test-dateutil.git/datepackage"strpkg"192.168.0.12/gitrepo/go-package-test-stringutil.git/stringpackage""fmt"" github.com/your-github-username/go-package-test-dateutil/datepackage"" github.com/your-github-username/go-package-test-stringutil/stringpackage")func main(){fmt.Println("github:" + stringpackage.ToUpperCase("test")))fmt.Println(" github:" + datepackage.GetTime().String())fmt.Println("local:" + strpkg.ToUpperCase("test")))fmt.Println("local:" + dpkg.GetTime().String())} 

请注意在两个新的 import 语句中使用别名 dpkg strpkg ,因为 import 语句包名称重复(import语句中的最后一个元素).

现在,您可以使用 go run main.go 来运行代码.

在guthub.com上使用私人存储库和在私人git服务器上使用存储库之间的主要区别:

  • 修改〜/.gitconfig
  • 在go get语句中添加.git后缀
  • go get import 语句中的主机名必须在其中包含一个点

使用私有存储库创建模块

创建与上述您创建的 packages/目录平行的目录结构.从上方复制 dateutil.go stringutil.go 文件.分别将package语句更改为 datemod stringmod .您稍后将创建 go.mod 文件.

 模块├──github的│──├──日期模块││├──dateutil.go││└──go.mod│└──stringmodule│├──go.mod│└──stringutil.go└──192.168.0.12├──日期模块│├──dateutil.go│└──go.mod└──stringmodule├──go.mod└──stringutil.go 

请注意,目录名称与软件包名称不同.程序包不必遵循目录名称.

通过在这些目录中执行以下命令,

生成 go.mod 文件:

github/datemodule/

  go mod init github.com/your-github-username/go-module-test-dateutilmod 

github/stringmodule/

  go mod init github.com/your-github-username/go-module-test-stringutilmod 

192.168.0.12/datemodule/

  go mod init 192.168.0.12/gitrepo/go-module-test-dateutil.git 

192.168.0.12/stringmodule/

  go mod init 192.168.0.12/gitrepo/go-module-test-stringutil.git 

在上面的四个 datemodule/ stringmodule/目录中,使用 git init 创建一个git repo并添加并提交文件.

在github.com上创建两个私有仓库,分别名为 go-module-test-dateutilmod go-module-test-stringutilmod .按照说明在 github/目录下的相应本地git仓库中设置遥控器.推送代码.

在您的私人git服务器上,使用创建两个git repos

  git init/home/myusername/gitrepo/go-module-test-dateutil --baregit init/home/myusername/gitrepo/go-module-test-stringutil --bare 

使用

192.168.0.12/目录下的相应git存储库中设置遥控器

  git remote add origin myusername@192.168.0.12:gitrepo/go-package-test-dateutilgit remote add origin myusername@192.168.0.12:gitrepo/go-package-test-stringutil 

按下代码.

现在您有四个不同的模块,您的github.com帐户和您的私有git服务器的存储库中各有两个.

在另一个目录中,创建一个 main.go 程序以使用以下模块:

 程序包主要进口 (datemodlocal"192.168.0.12/gitrepo/go-module-test-dateutil.git"stringmodlocal"192.168.0.12/gitrepo/go-module-test-stringutil.git";"fmt"" github.com/your-github-username/go-module-test-dateutilmod"" github.com/your-github-username/go-module-test-stringutilmod)func main(){fmt.Println("github:" + stringmod.ToUpperCase("test")))fmt.Println(" github:" + datemod.GetTime().String())fmt.Println("local:" + stringmodlocal.ToUpperCase("test")))fmt.Println("local:" + datemodlocal.GetTime().String())fmt.Println("local toString:" + datemodlocal.GetTimeStr())} 

要使用带有私有存储库的模块,我们必须设置 GOPRIVATE

  go env -w GOPRIVATE = 192.168.0.12/gitrepo/*,github.com/your-github-username/* 

设置了 GOPRIVATE 后,将直接从指定的git存储库中提取模块,而不是转到公共代理服务器.

现在运行

  $ go mod init模块驱动程序go:创建新的go.mod:模块module-driver$ cat go.mod模块模块驱动程序去1.15 

现在执行 main.go .在运行代码之前,它将从github.com和您的私有git服务器下载模块:

  $ go运行main.go前往:为软件包github.com/dwschulze/go-module-test-dateutilmod查找模块转到:查找软件包github.com/dwschulze/go-module-test-stringutilmod的模块go:查找软件包192.168.0.12/gitrepo/go-module-test-stringutil.git的模块go:查找软件包192.168.0.12/gitrepo/go-module-test-dateutil.git的模块前往:下载192.168.0.12/gitrepo/go-module-test-stringutil.git v0.0.1前往:下载192.168.0.12/gitrepo/go-module-test-dateutil.git v0.0.3前往:下载github.com/dwschulze/go-module-test-dateutilmod v0.0.1前往:下载github.com/dwschulze/go-module-test-stringutilmod v0.0.1前往:在192.168.0.12/gitrepo/go-module-test-dateutil.git v0.0.3中找到192.168.0.12/gitrepo/go-module-test-dateutil.git转到:在192.168.0.12/gitrepo/go-module-test-stringutil.git v0.0.1中找到192.168.0.12/gitrepo/go-module-test-stringutil.git前往:在github.com/dwschulze/go-module-test-dateutilmod v0.0.1中找到了github.com/dwschulze/go-module-test-dateutilmod前往:在github.com/dwschulze/go-module-test-stringutilmod v0.0.1中找到github.com/dwschulze/go-module-test-stringutilmodgithub:测试github:2020-12-08 07:57:02.969147007 -0700 MST本地:TEST本地:2020-12-08 07:57:02.969220121 -0700 MST本地toString:2020-12-08 07:57:02.969222359 -0700 MST(dev2分支)本地GetTimeStr3:2020-12-08 07:57:02.96925053 -0700 MST(dev2分支) 

在运行代码之前,您不必运行 go get 来下载这些模块. go.mod 也已修改

  $ cat go.mod模块模块驱动程序去1.15要求 (192.168.0.12/gitrepo/go-module-test-dateutil.git v0.0.3//间接192.168.0.12/gitrepo/go-module-test-stringutil.git v0.0.1//间接github.com/dwschulze/go-module-test-dateutilmod v0.0.1//间接github.com/dwschulze/go-module-test-stringutilmod v0.0.1//间接) 

您可以通过go get下载模块来测试您的环境设置是否正确:

 去获取192.168.0.12/gitrepo/go-module-test-dateutil.git去获取192.168.0.12/gitrepo/go-module-test-stringutil.git去获取github.com/your-github-username/go-module-test-dateutilmod去获取github.com/your-github-username/go-module-test-stringutilmod 

再次注意在私有git服务器的存储库路径上使用 .git 后缀.如果没有此 go get ,它将使用https而不是git(它将使用ssh).

运行 go get 修改 go.mod 文件.有些人说您可以手动修改 go.mod 文件,而其他人则说您不应该手动编辑 go.mod ,而应使用 go get 进行任何修改.

使用 go get go main.go 下载模块代码会将代码下载到 $ GOPATH/pkg/mod 中.由于未指定任何版本,它将为该模块提取最新的语义版本标记.

>

语义版本控制是通过仅作为提交的标签完成的.标签与分支无关,因此,如果最新的 semver 是在master以外的分支上完成的,它将提取该版本.

要指定特定版本,请使用

 获取192.168.0.12/gitrepo/go-module-test-dateutil.git@v0.0.1 

这将更新 go.mod 中的条目(如果已经存在)

您应该能够删除 $ GOPATH/pkg/mod/目录,并再次执行 go run main.go .您将看到Go在运行代码之前下载了所需的模块.

使用私有存储库中的模块时的主要区别

  • 修改〜/.gitconfig
  • 在专用服务器上为该回购添加.git后缀
  • 私有服务器的主机名中必须带有点或使用其IP地址
  • 使用 go env -w GOPRIVATE = ...
  • 设置 GOPRIVATE

在其他分支上使用代码

语义版本标记与分支无关,但是在一种情况下, go get 可以使用分支.如果您想从分支中获取最新提交,可以像这样添加 @branchname :

 去获取192.168.0.12/gitrepo/go-module-test-dateutil.git@branchname 

如果分支上的最新提交没有 semver 标签,则 go get 将使用下一个 semver 标签创建一个伪版本.数字和时间戳和哈希值.

常见问题解答

下载模块时 410 Gone 错误是什么意思?

 开始获取github.com/your-github-username/go-module-test-dateutilmod前往:下载github.com/your-github-username/go-module-test-dateutilmod v0.0.1去获取github.com/your-github-username/go-module-test-dateutilmod:github.com/your-github-username/go-module-test-dateutilmod@v0.0.1:验证模块:github.com/your-github-username/go-module-test-dateutilmod@v0.0.1:阅读https://sum.golang.org/lookup/github.com/your-github-username/go-module-test-dateutilmod@v0.0.1:410消失服务器响应:找不到:github.com/your-github-username/go-module-test-dateutilmod@v0.0.1:无效版本:未知修订版v0.0.1 

如果未设置GOPRIVATE,则可能会发生这种情况.Go尝试从Go公共代理检索您的(私有)模块,但在那里找不到它们.

资源

使用来自公共git托管服务的私有存储库.演示GOPRIVATE和GOPROXY如何工作.

I've got a git repo on a local server. I can clone from it with git clone user@10.xxx.yyy.zzz:/srv/git/liqid.git where the user has ssh access and read/write to the git/ directory.

When I try to use it with go get -v user@10.xxx.yyy.zzz:/srv/git/liqid.git it gives

go: cannot use path@version syntax in GOPATH mode

I've tried various other combinations like leaving out the :, but they all fail.

Can go get work with a repo on a local server?

解决方案

Here's what I've learned about using go get for packages and modules when the git repo is on a private server. I hope it helps someone else to have all the steps documented in one place.

Using Packages And Modules With git Repositories on Private Servers

Here are all the steps needed to use Go packages and modules with git repositories on private servers. These are servers with IP addresses such as 10.xxx.yyy.zzz or 192.168.xxx.yyy. No github or gitlab is assumed to exist on these servers so there is no web server running on port 80, which is the assumption that go get is based on. Your private server only needs to have sshd running.

Install the latest version of Go and be sure the GOPATH is set. The first element in your GOPATH is where the code from these examples will be downloaded to.

You'll need another computer on your network where you will create git repositories. This will be your private git server.

If you want to skip all of the setup steps and example code you can check the two Key Differences sections that list the differences when using packages or modules with a private server instead of a public git repository.

The source code

Put dateutil.go and stringutil.go in the directory structures shown below for both packages and modules.

dateutil.go:

package datepackage

import "time"

func GetTime() time.Time {
    return time.Now().Local()
}

stringutil.go:

package stringpackage

import "strings"

func ToUpperCase(s string) string {

    return strings.ToUpper(s)
}

main.go (put main.go somewhere outside of the directory structure shown below):

package main

import (
    "fmt"
    "github.com/your-github-username/go-package-test-dateutil/datepackage"
    "github.com/your-github-username/go-package-test-stringutil/stringpackage"
)

func main() {

    fmt.Println("github:  " + stringpackage.ToUpperCase("test"))
    fmt.Println("github:  " + datepackage.GetTime().String())
}

These files can be found on github.com

git@github.com:dwschulze/go-package-test-dateutil.git

git@github.com:dwschulze/go-package-test-stringutil.git

git@github.com:dwschulze/go-module-package-test-drivers.git

Create Packages Using the GOPATH Convention

Create a directory structure like this outside of your GOPATH and add the files above. This follows the GOPATH convention, but you don't need these files in you GOPATH.

package
├── github
│   ├── dateutil
│   │   └── src
│   │       └── datepackage
│   │           └── dateutil.go
│   └── stringutil
│       └── src
│           └── stringpackage
│               └── stringutil.go
└── your-local-git-repo-hostname
    ├── dateutil
    │   └── src
    │       └── datepackage
    │           └── dateutil.go
    └── stringutil
        └── src
            └── stringpackage
                └── stringutil.go

your-local-git-repo-hostname is the hostname or IP address of your private git server where you'll create git repos (not the machine where you currently have this code). There is an undocumented requirement that the hostname for the go get command have a . in it. If the hostname of your private git server doesn't have a . in it then use its IP address. Add ssh key access to your private git server with ssh-copy-id.

Using a Private repo on github

We'll start with the simplest case, using packages from github.com. You'll need a github account with ssh key access setup.

Create git repos in the two src/ directories under github/ that you created above using git init

package
├── github
│   ├── dateutil
│   │   └── src
│   │       └── ...
│   └── stringutil
│       └── src
|           └── ...

Add and commit the datepackage/ and stringpackage/ dirs respectively to your git repos.

Create two private github repos named go-package-test-dateutil and go-package-test-stringutil in your account on github.com. Follow the instructions to set the remotes in your local git repos in your src/ directories to their respective github repositories. Push the code.

Since your repos are private you'll have to use ssh public key access to download the code. Since go get uses https by default you'll need to add this to your ~/.gitconfig file:

git config --global url."git@github.com:".insteadOf "https://github.com/"

Run these commands to put the code you just pushed to github.com into your GOPATH:

go get github.com/your-github-username/go-package-test-stringutil/stringpackage/

go get github.com/your-github-username/go-package-test-dateutil/datepackage/

The packages get downloaded into the pkg/ and src/ directories of the first element in your GOPATH.

In the directory with the main.go file you created above type go run main.go and the results will be printed to the console.

Create Git Repositories on Your Private Server

Now you'll create git repos on your private git server. To keep this simple you'll just use git init --bare. There's no need to install github or gitlab. On your private git server you'll need to have sshd running and have ssh key access from the machine where your code is. The new repos will be in /home/myusername/gitrepo.

git init /home/myusername/gitrepo/go-package-test-dateutil --bare

git init /home/myusername/gitrepo/go-package-test-stringutil --bare

The go get command has an undocumented requirement that the hostname have a . in it. If the hostname of your private git server doesn't have a . in it then use its IP address, which is what I'll do throughout the rest of this example. Assume the machine's IP address is 192.168.0.12

Copy the dateutil.go and stringutil.go files that you created earlier into the directories under your-local-git-repo-hostname:

package
├── github
|   ...
└── your-local-git-repo-hostname
    ├── dateutil
    │   └── src
    │       └── datepackage
    │           └── dateutil.go
    └── stringutil
        └── src
            └── stringpackage
                └── stringutil.go

In the two src/ directories create local git repos as you did before and add and commit the code. Set the remotes to the git repo that you created on your private git server

git remote add origin myusername@your-local-git-repo-hostname:gitrepo/go-package-test-dateutil


git remote add origin myusername@your-local-git-repo-hostname:gitrepo/go-package-test-stringutil

You'll need another entry in ~/.gitconfig for your private git server:

git config --global url."myusername@your-local-git-repo-hostname:".insteadOf "https://192.168.0.12/"

Now push the code. Note that this code is not yet in your GOPATH.

Use go get to retrieve the code from your private git server. It's necessary to add a ".git" suffix to the directory name where you created the git repos with git init --bare on your private git server. That tells go get that this is a git repository rather than some other version control system.

go get 192.168.0.12/gitrepo/go-package-test-stringutil.git/stringpackage


go get 192.168.0.12/gitrepo/go-package-test-dateutil.git/datepackage

The packages get downloaded into the pkg/ and src/ directories of the first element in your GOPATH.

└── src
    ├── 192.168.0.12
    │   └── gitrepo
    │       ├── go-module-test-dateutil.git
    │       │   ├── dateutil.go
    │       │   └── go.mod
    │       ├── go-package-test-dateutil.git
    │       │   └── datepackage
    │       │       └── dateutil.go
    │       └── go-package-test-stringutil.git
    │           └── stringpackage
    │               └── stringutil.go

In your main.go code add two import statements to the packages on your private git server

package main

import (
    dpkg "192.168.0.12/gitrepo/go-package-test-dateutil.git/datepackage"
    strpkg "192.168.0.12/gitrepo/go-package-test-stringutil.git/stringpackage"
    "fmt"
    "github.com/your-github-username/go-package-test-dateutil/datepackage"
    "github.com/your-github-username/go-package-test-stringutil/stringpackage"
)

func main() {

    fmt.Println("github:  " + stringpackage.ToUpperCase("test"))
    fmt.Println("github:  " + datepackage.GetTime().String())
    fmt.Println("local:  " + strpkg.ToUpperCase("test"))
    fmt.Println("local:  " + dpkg.GetTime().String())
}

Note the use of the aliases dpkg and strpkg in the two new import statements because the import statements have duplicate package names (the last element in the import statement).

Now you can run the code with go run main.go.

Key differences between using a private repo on guthub.com and a repo on a private git server:

  • Modifying ~/.gitconfig
  • Adding the .git suffix in go get statements
  • The hostname in go get and import statements must have a dot in it

Creating Modules Using Private Repositories

Create a directory structure like this parallel to the packages/ directory you created above. Copy the dateutil.go and stringutil.go files from above. Change the package statements to datemod and stringmod respectively. You'll create the go.mod files later.

module
├── github
│   ├── datemodule
│   │   ├── dateutil.go
│   │   └── go.mod
│   └── stringmodule
│       ├── go.mod
│       └── stringutil.go
└── 192.168.0.12
    ├── datemodule
    │   ├── dateutil.go
    │   └── go.mod
    └── stringmodule
        ├── go.mod
        └── stringutil.go

Note that the directory names are different from the package names. Packages don't have to follow directory names.

Generate the go.mod files by executing the following commands in these directories:

In github/datemodule/

go mod init github.com/your-github-username/go-module-test-dateutilmod

In github/stringmodule/

go mod init github.com/your-github-username/go-module-test-stringutilmod

In 192.168.0.12/datemodule/

go mod init 192.168.0.12/gitrepo/go-module-test-dateutil.git

In 192.168.0.12/stringmodule/

go mod init 192.168.0.12/gitrepo/go-module-test-stringutil.git

In the four datemodule/ and stringmodule/ directories above create a git repo with git init and add and commit the files.

Create two private repos on github.com named go-module-test-dateutilmod and go-module-test-stringutilmod. Follow the instructions to set the remotes in your respective local git repos under the github/ directory. Push the code.

On your private git server create two git repos with

git init /home/myusername/gitrepo/go-module-test-dateutil --bare

git init /home/myusername/gitrepo/go-module-test-stringutil --bare

Set the remotes in the respective git repos under 192.168.0.12/ directory with

git remote add origin myusername@192.168.0.12:gitrepo/go-package-test-dateutil


git remote add origin myusername@192.168.0.12:gitrepo/go-package-test-stringutil

Push the code.

Now you have four different modules, two each in repositories in your github.com account and your private git server.

In another directory create a main.go program to use these modules:

package main

import (
    datemodlocal "192.168.0.12/gitrepo/go-module-test-dateutil.git"
    stringmodlocal "192.168.0.12/gitrepo/go-module-test-stringutil.git"
    "fmt"
    "github.com/your-github-username/go-module-test-dateutilmod"
    "github.com/your-github-username/go-module-test-stringutilmod"
)

func main() {

    fmt.Println("github:  " + stringmod.ToUpperCase("test"))
    fmt.Println("github:  " + datemod.GetTime().String())
    fmt.Println("local:  " + stringmodlocal.ToUpperCase("test"))
    fmt.Println("local:  " + datemodlocal.GetTime().String())
    fmt.Println("local toString:  " + datemodlocal.GetTimeStr())
}

To use modules with private repos we have to set GOPRIVATE

go env -w GOPRIVATE=192.168.0.12/gitrepo/*,github.com/your-github-username/*

When GOPRIVATE is set modules will be pulled directly from the specified git repos instead of the Go public proxy.

Now run

$ go mod init module-driver
  go: creating new go.mod: module module-driver
$ cat go.mod
  module module-driver

  go 1.15

Now execute main.go. It will download the modules from github.com and your private git server before running the code:

$ go run main.go
go: finding module for package github.com/dwschulze/go-module-test-dateutilmod
go: finding module for package github.com/dwschulze/go-module-test-stringutilmod
go: finding module for package 192.168.0.12/gitrepo/go-module-test-stringutil.git
go: finding module for package 192.168.0.12/gitrepo/go-module-test-dateutil.git
go: downloading 192.168.0.12/gitrepo/go-module-test-stringutil.git v0.0.1
go: downloading 192.168.0.12/gitrepo/go-module-test-dateutil.git v0.0.3
go: downloading github.com/dwschulze/go-module-test-dateutilmod v0.0.1
go: downloading github.com/dwschulze/go-module-test-stringutilmod v0.0.1
go: found 192.168.0.12/gitrepo/go-module-test-dateutil.git in 192.168.0.12/gitrepo/go-module-test-dateutil.git v0.0.3
go: found 192.168.0.12/gitrepo/go-module-test-stringutil.git in 192.168.0.12/gitrepo/go-module-test-stringutil.git v0.0.1
go: found github.com/dwschulze/go-module-test-dateutilmod in github.com/dwschulze/go-module-test-dateutilmod v0.0.1
go: found github.com/dwschulze/go-module-test-stringutilmod in github.com/dwschulze/go-module-test-stringutilmod v0.0.1
github:  TEST
github:  2020-12-08 07:57:02.969147007 -0700 MST
local:  TEST
local:  2020-12-08 07:57:02.969220121 -0700 MST
local toString:  2020-12-08 07:57:02.969222359 -0700 MST  (dev2 branch)
local GetTimeStr3:  2020-12-08 07:57:02.96925053 -0700 MST  (dev2 branch)

You didn't have to run go get to download these modules before running the code. The go.mod was also modified

$ cat go.mod
module module-driver

go 1.15

require (
    192.168.0.12/gitrepo/go-module-test-dateutil.git v0.0.3 // indirect
    192.168.0.12/gitrepo/go-module-test-stringutil.git v0.0.1 // indirect
    github.com/dwschulze/go-module-test-dateutilmod v0.0.1 // indirect
    github.com/dwschulze/go-module-test-stringutilmod v0.0.1 // indirect
)

You can download your modules with go get to test that your environment is set up correctly:

go get 192.168.0.12/gitrepo/go-module-test-dateutil.git
go get 192.168.0.12/gitrepo/go-module-test-stringutil.git
go get github.com/your-github-username/go-module-test-dateutilmod
go get github.com/your-github-username/go-module-test-stringutilmod

Again note the use of the .git suffix on the repository path for your private git server. Without this go get will use https instead of git (which would use ssh).

Running go get modifies the go.mod file. While some people say you can manually modify the go.mod file others say that you should not edit go.mod manually and instead use go get to make any modifications.

Downloading the module code either with go get or go run main.go downloads the code into $GOPATH/pkg/mod. Since no version was specified it will pull the latest semantic version tag for that module.

Semantic versioning is done with tags which are just a commit. Tags are independent of branches so if the latest semver was done on a branch other than master it will pull that version.

To specify a specific version use

go get 192.168.0.12/gitrepo/go-module-test-dateutil.git@v0.0.1

This will update the entry in go.mod if one already exists

You should be able to delete $GOPATH/pkg/mod/ directory and do go run main.go again. You'll see Go downloading the needed modules before running the code.

Key Differences When Using Modules From a Private Repository

  • Modifying ~/.gitconfig
  • Adding the .git suffix for the repos on your private server
  • The hostname of your private server must have a dot in it, or use its IP address
  • Set GOPRIVATE with go env -w GOPRIVATE=...

Using code on a different branch

Semantic version tags are independent of branches, but there is one case where go get can use a branch. If you want to go get the latest commit from a branch you can append @branchname like this:

go get 192.168.0.12/gitrepo/go-module-test-dateutil.git@branchname

If the latest commit on the branch does not have a semver tag go get will create a pseudo-version using the next semver tag number and a timestammp and hash.

FAQs

What does a 410 Gone error mean when downloading a module?

go get github.com/your-github-username/go-module-test-dateutilmod
go: downloading github.com/your-github-username/go-module-test-dateutilmod v0.0.1
go get github.com/your-github-username/go-module-test-dateutilmod: github.com/your-github-username/go-module-test-dateutilmod@v0.0.1: verifying module: github.com/your-github-username/go-module-test-dateutilmod@v0.0.1: reading https://sum.golang.org/lookup/github.com/your-github-username/go-module-test-dateutilmod@v0.0.1: 410 Gone
    server response: not found: github.com/your-github-username/go-module-test-dateutilmod@v0.0.1: invalid version: unknown revision v0.0.1

This can happen if you don't have GOPRIVATE set. Go tries to retrieve your (private) modules from the Go public proxy, but it can't find them there.

Resources

Using private repos from public git hosting services. A demonstration of how GOPRIVATE and GOPROXY work.

这篇关于我该如何在本地服务器上使用仓库进行工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆