golang check package dependencies on a single repository - go

I am working on a golang project which have a few packages (folders). My purpose is to pull out a certain package into a separate module, but before I do this I want to see the package dependencies. Is there any tool out there which I can run to view the package dependencies. I am not talking about 3rd party modules but just simple folders in the same repo. I checked around for a few tools but could not find any.

go list -f {{.Deps}} path/to/your/package gives you all direct and indirect dependencies.
For Go modules you can do go mod graph or go list -m all.

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

How do I debug Go dependency packages?

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.

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.

Get missing imports in batches

When I clone a project written in golang, it is normal that a lot of imports like
'github.com/XXXX' are missing. Is there any way to get these imports in batches by a command? or I am suppose to get them one by one.
You should use go get to get "remote" packages. Quoting from Command go: Download and install packages and dependencies
Get downloads the packages named by the import paths, along with their dependencies. It then installs the named packages, like 'go install'.
You may use the -v flag in all of the following commands, and so you will see what go get is doing under the hood.
You may use the -d flag if you just want to download the packages but you do not want to install them.
The examples use the example remote package github.com/somebody/somepackage, but obviously it works for other packages hosted outside of github.com.
For more information, see the official doc: Command go, or type go help get.
To get a single package with all the dependencies of that package and install them, use
go get github.com/somebody/somepackage
To get a package with all its dependencies, and all other packages rooted at that path (along with their dependencies), and install all of them, use:
go get github.com/somebody/somepackage/...
Quoting from Command go:
An import path is a pattern if it includes one or more "..." wildcards, each of which can match any string, including the empty string and strings containing slashes. Such a pattern expands to all package directories found in the GOPATH trees with names matching the patterns.
To get a package with all its dependencies (and "subpackages") including dependencies of tests, and install all of them, use:
go get -t github.com/somebody/somepackage/...
To update a package you already have, use:
go get -u github.com/somebody/somepackage/...
To fetch dependencies of a package you already have (which is not necessarily from a remote location):
go get path/to/package/name/...
Or go to its folder and then you may use a relative path:
go get ./...
A lot of golang projects now use dependency management so you should look for that first. e.g a Glide.lock (glide) or Gopkg.lock (dep - the way people are moving now) file present in the root of the project.
https://github.com/golang/dep
https://golang.github.io/dep
if dep is used and you have it installed then dep ensure will set the dependencies up for you and make sure you get the versions the author intended
if a project is not using dependency management you can just get the packages with go get ./... but I don't think you will be guaranteed the correct versions (e.g if the author was pinned to a version tag for a dep)
If you run dep init it sets up dep on a project and will attempt to resolve the correct versions, however this doesnt always work if the stars dont align (e.g I have seen issues with dependencies using gopkg.in)
try using go get ./... in root of your project

How to import a third party library to a specific folder

Being very new to Go, I'm trying to import a third party library into a vendor folder. I follow the instructions given by the Go docs, but didn't find anything about third party libraries.
Update (2019)
The Go environment is slowly starting to move away from tools like dep and towards the native Go tooling around modules. While explaining models is outside the scope of this answer, you can look into modules from the following places:
https://blog.golang.org/modules2019
https://github.com/golang/go/wiki/Modules
tldr
Install dep: go get -u github.com/golang/dep/cmd/dep
In your project run: dep init
answer
The easiest way to solve this problem in my opinion is using the dep dependency management tool. This tool is very widely in use and is very easy to use. Here is a typical workflow:
First you should install the dep program.
go get -u github.com/golang/dep/cmd/dep
Now you have access to the dep command. Full documentation can be found here: https://golang.github.io/dep/
This is how you get 3rd party libraries into your vendor directory. In the example below we will use the url router github.com/gorilla/mux.
First, in you code import the libraries like normal.
package main
import "github.com/gorilla/mux"
func main {
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
r.HandleFunc("/products", ProductsHandler)
r.HandleFunc("/articles", ArticlesHandler)
http.Handle("/", r)
}
Now all we have to do to get it sed up is run the dep init command. This will look for all of your imports and create a vendor directory for you with all of your needed dependencies. Note that dep automatically analyzes your imports.
Dep in depth
Once you have initialized dep you can start work on your project as normal. When you add a new library you can run the dep ensure command to get the newly added 3rd party libraries in the vendor directory.
In addition, dep gives you the capability to lock down on particular versions of 3rd party libraries. dep init initializes your project with two files: Gopkg.toml and Gopkg.lock. The Gopkg.toml file contains assertions about which dependencies will be at what version. For example, if you wantted the gorilla mux library to stay at version v1.4.0, you could add the following line to your Gopkg.toml:
[[constraint]]
name = "github.com/gorilla/mux"
version = "=v1.4.0"
Dep also has functionality to upgrade dependencies, remove unused dependencies from vendor, and much more. Look at the documentation for more details. https://golang.github.io/dep/

Resources