Greg C
Greg C

Reputation: 153

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. 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:

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.

When I try the "go get" command above, I get this error message:

$ 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. That produced a sql.a that ran, but didn't work.

Upvotes: 2

Views: 1080

Answers (2)

Mr_Pink
Mr_Pink

Reputation: 109417

You can either build Go from source, and then modify the standard library in place, or you can vendor that particular package.

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

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.

Upvotes: 5

Ario
Ario

Reputation: 1940

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. If you want to see the errors you can use the error type.

e.g sql.Open returns *sql.DB, error

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

Upvotes: 0

Related Questions