简体   繁体   English

拉出单行标准包装的副本以进行修改

[英]Pull a copy of a single go standard package for modification

I have an idea for a minor addition I would like to make to the database/sql go package. 我有一个想法,想对数据库/ sql go软件包做些小的补充。 I would like to try my change out to see how it works. 我想尝试一下更改,以了解其工作原理。

I thought that I would be able to execute this command to get a copy of the source in database/sql that I could play with: 我以为我可以执行此命令来获取可以在数据库/ sql中使用的源代码副本:

go get github.com/golang/go/tree/master/src/database/sql

Then, I was going to change my import statement from 然后,我打算将导入声明从

import "database/sql"

to

import "github.com/golang/go/tree/master/src/database/sql"

and put a debugging Printf in my local copy of the code to confirm that the new code I just pulled was being executed rather than the code in /usr/local/go. 并将调试Printf放入代码的本地副本中,以确认刚刚执行的新代码正在执行,而不是/ usr / local / go中的代码。

When I try the "go get" command above, I get this error message: 当我尝试上面的“ go get”命令时,出现以下错误消息:

$ go get github.com/golang/go/tree/master/src/database/sql
package github.com/golang/go/tree/master/src/database/sql: cannot find package "github.com/golang/go/tree/master/src/database/sql" in any of:
    /usr/local/go/src/github.com/golang/go/tree/master/src/database/sql (from $GOROOT)
    /Users/me/go/src/github.com/golang/go/tree/master/src/database/sql (from $GOPATH)
$ echo $GOPATH
/Users/me/go
$

Why doesn't this work? 为什么不起作用?

I also tried doing this with the source at 我也尝试使用以下来源进行此操作

https://go.googlesource.com/go/+/release-branch.go1.6/src/database/sql

That didn't work either. 那也不起作用。 Then I tried downloading the tgz of the source from the link above, and untarring those files in my own $GOPATH/src/github.com/database/sql folder, then go building them there. 然后,我尝试从上面的链接下载源代码的tgz,并在我自己的$ GOPATH / src / github.com / database / sql文件夹中解压缩这些文件,然后在此处进行构建。 That produced a sql.a that ran, but didn't work. 那产生了一个运行的sql.a,但是没有用。

You can either build Go from source, and then modify the standard library in place, or you can vendor that particular package. 您可以从源代码构建Go,然后就地修改标准库,也可以出售该特定程序包。

If you have Go installed from source, after editing the package you can install the new version just like any other package with go install database/sql 如果您从源代码安装了Go,则在编辑软件包后,可以像使用go install database/sql任何其他软件包一样安装新版本

If you copy the database/sql package into a vendor directory, that copy will be built and imported in place of the version from the standard library. 如果将database/sql软件包复制到vendor目录中,则将代替标准库中的版本来构建和导入该副本。

As Godoc says 正如戈多克所说

 get download and install packages and dependencies 

You just can get packages and dependencies. 您只可以获取软件包和依赖项。 But you want to get a folder! 但是你想得到一个文件夹! You can download its zip file from github and copy it to that destination. 您可以从github下载其zip文件,然后将其复制到该目标位置。 If you want to see the errors you can use the error type. 如果要查看错误,可以使用错误类型。

eg sql.Open returns *sql.DB, error 例如sql.Open返回* sql.DB,错误

db, err := sql.Open("mysql", "astaxie:astaxie@/test?charset=utf8")
if err != nil {
    fmt.Println(err)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM