How do I debug Go dependency packages? - go

Say my Go project depends on package example.com/foo. I am using Go 1.12, so the dependency is automatically pulled in by Go modules. That dependency has some problems/bugs, I want to add logs in the source code.
I can find the source code of the dependency on GitHub but I don't know how to make it into my project for debugging purpose.

First fetch all the dependency packages into the vendor folder.
go mod vendor
Then, change the source code in that and build your project by specifying to look into vendor folder.
go build -mod=vendor
or
go run -mod=vendor myapp.go

You can use replace directive:
replace example.com/original/import/path => /your/forked/import/path

Go module fetches packages into $GOPATH/pkg/mod you can change the source code there or using the vendor option of go mod to pull the packages into vendor folder and then start coding there.

Related

how to reference a package in another package having GO111MODULE=on in go

If i have the below structure:
HelperFolder
Library1
lib1.go
Library2
lib2.go
file2.go
Where lib1.go imports Library2.
I have executed go mod init in library2 and then go mod tidy and then go build and all is fine.
But I can't get Library1 built.
I do not want to install any of the libraries, or place the libraries in gopath (i have placed the HelperFolder in a different path) but i always get the error in building library1 that package2 is not in GOROOT(C:\program files\go\src\package2)
What am I missing?
any help is appreciated
A Go module may contain many packages. The main reason to define multiple modules is so that you can version and release the packages in those modules on separate release schedules. For local-only (unpublished, unversioned) packages, it is pretty much always simpler to instead use a single unified module.
That is: run go mod init just once in HelperFolder rather than twice in the individual Library folders.
As #Zyl noted in his comment, i used the following go commands:
go mod edit replace HelperFolder/Lib2 => D:\folderPath\HelperFolder\Lib2
Then
go get HelperFolder/Lib2

Where is the go project working directory when using modules?

I am trying to download, make some tweaks and build a golang project from GitHub. The project's instructions are:
go get github.com/<vendor>/<projectName>
cd src/github.com/<vendor>/<projectName>
go build .
That used to work in the past — before enabling Go Modules.
Now I have GO111MODULE=on (go version go1.15.4 linux/amd64). When running the first command, go downloads the project as a module and all its dependencies.
But then there is no src/github.com/<vendor>/<projectName> folder anymore. Moreover, the is no folder named <projectName> anywhere in the system.
Instead, there is folder pkg/mod/github.com/<vendor> which contains the project folder with weird symbols in its name (exclamation marks etc.) and version identifiers.
How do I get the project folder available for making tweaks and builds?
As pointed by #Volker, good old git clone should be used.
It turns out that it should be used instead of go get github.com/<vendor>/<projectName> (no idea why the project vendor recommends that):
git clone git://github.com/<vendor>/<projectName>
cd <projectName>
go get ./...
# do tweaks here
go build .
If your goal is tweaks, the easiest way it use to use go mod vendor.
https://golang.org/ref/mod#go-mod-vendor
The go mod vendor command constructs a directory named vendor in the main module's root directory that contains copies of all packages needed to support builds and tests of packages in the main module

go mod vendor without update to latest

I’m trying to figure out if it’s possible to run go mod vendor without the go tool updating my go.mod file.
I specifically go get package/subpackage#commit and commit my go.mod with the correct version.
Then I run go mod vendor and it automatically bumps the version of the package that I just specifically set.
I’ve looked at this page to no avail: https://github.com/golang/go/wiki/Modules#how-do-i-use-vendoring-with-modules-is-vendoring-going-away
I need to use vendor because I run a script that edits some of the vendored deps., I’m looking at the following build flow:
GO111MODULE=on go get package/subpackge#commit
GO111MODULE=on go mod vendor
./Script/patch_vendors.sh --write
GO111MODULE=off go build
My other option is modifying the copied source wherever go mod vendor donwloads it to, but
not sure how to approach that.
Thanks in advance
Per https://tip.golang.org/cmd/go/#hdr-Maintaining_module_requirements:
The go command itself automatically updates the go.mod file to maintain a standard formatting and the accuracy of require statements.
Any go command that finds an unfamiliar import will look up the module containing that import and add the latest version of that module to go.mod automatically. […]
Any go command can determine that a module requirement is missing and must be added […].
The go mod vendor command copies in all of the transitive imports of your packages and their tests, so it will automatically update the go.mod file to ensure that all of the imported packages are present.
So the problem here is likely that the commit you've selected for package/subpackage fails to provide some package that appears in the transitive imports of your program. If that is correct, you should find that go list all, go test all, and go mod tidy all make that same edit to your module's requirements.

Automatically import 3rd party go packages

I was wondering if there is a way to automatically download all the imports.
So let's assume I need to use github.com/gorilla/mux and several other packages in my code base. Should I manually go to my ~/go/src and run go get repo or is there a smarter way of doing dependency management. I am using Goland IDE for my development.
if there is a way to automatically download all the imports
You can download all imported pkgs and their dependencies by running go get from the command line.
I am using Goland IDE for my development
I'm using Goland too. When imports can't be found (ie the import path is highlighted in red), you can place your typing caret over it and press alt + enter and select go get ... from the popup window to automatically import.
There are several approaces:
Simply go get github.com/gorilla/mux which will download sources in your $GOPATH and will be resolved automatically when compiling
Use dependency management (godep, glide[deprecated])
Use modules (experimental feature in Go 1.11 - Module. Check more here)
If you want a good and stable solution, use dep (.First you have to install it, then run:
cd $GOPATH/src/path/to/project
dep init
dep ensure -add github.com/gorilla/mux
You will see a new folder vendor in your project and 2 new dependency configuration files Gopkg.lock and Gopkg.toml.
Read more about godep here.
Then run your main file as usual.
You can use dep package manager which will go through your code and automatically import all the packages you use in you code. If you are working with >go1.11 I would suggest to use newly added go mod.

cannot find vendor directory under golang project

My golang version is go1.10.2 linux/amd64. I can build and run my go project(under gopath/src) without any problem but I cannot see vendor directory under my project folder. I would like to know if the vendor folder is a hidden directory? What are the possible reasons the vendor folder is not generated?
Vendor directory is used as an alternative to GOPATH when resolving dependencies. A dependency is first looked up in /vendor then in GOPATH then in GOROOT.
If you go get all your dependencies they'll be in GOPATH/src instead of /vendor.
To start adding project specific dependencies to vendor dir you need to use a dependency manager such as glide or dep or manually copy everything to /vendor.
This SO answer goes into more detail on using vendor dir in Go - https://stackoverflow.com/a/37238226/1589165

Resources