go modules used in replace usage

For some unknown reason, not all packets can directly get to go get, then we need to replace the function of a go modules. (Of course, most of the problems can be solved hang a ladder, but we can also have other options)

 

Use replace the replacement package

replace the name suggests, is replaced with a new package to another package, they can be a different package, or may be different versions of the same package. Look at the basic syntax:

go mod edit -replace=old[@v]=new[@v]

old is to be replaced in the package, new is used to replace the package.

Here are some points to note:

  • replace should be performed immediately after the introduction of new dependencies, so as not to use the old package when go tools automatically update the mod file may lead to failure
  • Behind the package version can not be omitted. (Edit all operations required version tag)
  • version can not be a master or latest, go get both available, but go mod edit unrecognizable error. (I do not know bug, although documentation can indicate so with the hope that go1.12 do point improvement measures)

For these reasons, we substitute a package of steps should look like this:

  1. First, go get new-package (if you know the version tag package, then this step can be omitted in fact, if you want to use the latest version and do not want to confirm the version number, you need this step)
  2. Then view go.mod, manually copy the version number of the new-package (if you know the version number, skip this step could not be very user-friendly, and perhaps the future will improve)
  3. Then go mod tidy or go build or use other go tools, they will go to get new-package and replace the old-package
  4. Finally, use your code directly in the name of old-package, golang will automatically recognize replace, then your program will actually use the new-package, replacing success

Here we still do an example using the example chromedp.

 

Examples

chromedp used golang.org/x/image, this package is not generally direct acquisition, but it has a mirror github.com/golang/image, so we use replace to replace it with mirrors.

Let's look at the situation if the dependent does not replace in case of:

Yes, we use the original package, of course, if you can not get to it, then the recording will not be coming.

Let's go get its mirror image:

# Master retrieves the latest the commit 
Go GET github.com/golang/image@master

Then we see the version number:

cat go.mod

With the version number, we will be able to replace the:

go edit mode -replace = golang.org / x / image @ v0.0.0-20180708004352-c73c2afc3b81 = github.com / golang / image @ v0.0.0-20180708004352-c73c2afc3b81

Now we look go.mod:

replace the information has been updated, and now we just go mod tidy or go build, our code can use the new-package.

The updated go.sum, has been replaced with a mirror-dependent:

For now, replace doing far less easy to go get that kind of humanity, but only after all the testing phase of the function, look at its performance in go1.12 it.

 

 

Use replace the replacement package

replace the name suggests, is replaced with a new package to another package, they can be a different package, or may be different versions of the same package. Look at the basic syntax:

go mod edit -replace=old[@v]=new[@v]

old is to be replaced in the package, new is used to replace the package.

Here are some points to note:

  • replace should be performed immediately after the introduction of new dependencies, so as not to use the old package when go tools automatically update the mod file may lead to failure
  • package后面的version不可省略。(edit所有操作都需要版本tag)
  • version不能是master或者latest,这两者go get可用,但是go mod edit不可识别,会报错。(不知道是不是bug,虽然文档里表示可以这么用,希望go1.12能做点完善措施)

基于以上原因,我们替换一个package的步骤应该是这样的:

  1. 首先go get new-package(如果你知道package的版本tag,那么这一步其实可以省略,如果想使用最新的版本而不想确认版本号,则需要这一步)
  2. 然后查看go.mod,手动复制new-package的版本号(如果你知道版本号,则跳过,这一步十分得不人性化,也许以后会改进)
  3. 接着go mod tidy或者go build或者使用其他的go tools,他们会去获取new-package然后替换掉old-package
  4. 最后,在你的代码里直接使用old-package的名字,golang会自动识别出replace,然后实际你的程序将会使用new-package,替换成功

下面我们仍然用chromedp的example做一个示例。

 

示例

chromedp使用了golang.org/x/image,这个package一般直连是获取不了的,但是它有一个github.com/golang/image的镜像,所以我们要用replace来用镜像替换它。

我们先来看看如果不replace的情况下的依赖情况:

没错,我们使用了原来的包,当然如果你无法获取到它的话是不会被记录进来的。

下面我们go get它的镜像:

# master表示获取最新的commit
go get github.com/golang/image@master

然后我们查看版本号:

cat go.mod

有了版本号,我们就能replace了:

go mod edit -replace=golang.org/x/[email protected]=github.com/golang/[email protected]

现在我们查看一下go.mod:

replace信息已经更新了,现在我们只要go mod tidy或者go build,我们的代码就可以使用new-package了。

更新后的go.sum,依赖已经替换成了镜像:

目前来看,replace做的远不如go get那样方便人性化,不过毕竟还只是测试阶段的功能,期待一下它在go1.12的表现吧。

 

Guess you like

Origin www.cnblogs.com/sunsky303/p/12150575.html