Is it possible to update local packages without running go install? - go

I am trying to import a local file into my main.go file and this tutorial (and other similar tutorials) says to run go install <path> in order to import that path as a package. This seems like a slow way to develop local packages because you would have to run go install <path> every time you want to see the changes in your local package.
Is there a faster way to import/update local packages? I am using gomon to auto-reload my code after updating it, so ideally, my code would auto-reload after updating a local package.

You should use go modules. The tutorial you mentioned appears to be older than the modules feature. In short: you can import a package, run go build, and any imported external package will automatically be downloaded for you as needed, no need to do a go get. Start here:
https://blog.golang.org/using-go-modules
https://github.com/golang/go/wiki/Modules

Related

Using modules, newly installed package cannot be referenced within project

go version go1.11.4 darwin/amd64
GOPATH has been unset but was previously set to $HOME/Development/go
Project path is under $HOME/code/
I’m able to successfully (no errors at least) install the go-sql-driver/mysql package using the command
go get github.com/go-sql-driver/mysql#v1
When I include a reference to that package in an import statement
import(
_ "github.com/go-sql-driver/mysql")
in my code I see the error
could not import github.com/go-sql-driver/mysql (can’t find import:
“github.com/go-sql-driver/mysql”)
I have run go mod init in my project root and it creates a go.mod file. When I run the go get command I see a require statement added to that file for the package. But it seems the files for the package get installed in the default $HOME/go directory (since I've unset GOPATH).
Should I be doing things differently so that my import statement can find the newly installed package? Using modules shouldn't all the packages be installed in the project path somewhere?
Should I be doing things differently so that my import statement can find the newly installed package?
No. With modules there is no need to install the packages upfront at all.
Using modules shouldn't all the packages be installed in the project path somewhere?
No. They get downloaded somewhere in some format and used from that location but they are not "installed" like in the old GOPATH variant of go get.
Show output of go env and what go mod vendor produces.
I'm pretty sure I was doing things wrong. I was able to resolve this after referencing the following closely the steps documented at golang modules wiki. The summary is that there is no need to "install" a package via 'go get'. Instead simply make sure your project is initialized to use modules using the 'go mod init' command and then include the package name in an import statement. The next build event will pull down the package and all its dependencies.

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.

How do I rebuild native Golang packages after I modify them?

I modified the net package and I want to use the modified version in my application but it keeps using the old code.
This works:
$ go install -a net
It wasn't rebuilding for me either, but the -a forces a rebuild even if the toolchain thinks the specified package is up to date.
In case someone is using Docker:
My changes wasn't having effect because I was editing the package outside the container. Only the code of our project is mapped by a volume inside the container, but not the third party packages.
Once I edited the package code inside the container, I do not even had to run a go install -a ... like proposed by #elimisteve. It worked just by recompiling the project.

Go vendoring outside $GOPATH

I have a project which is built in Node.js/Express.js. I want to start to rewrite this to go/iris framework. I don't want to re-factor everything into my $GOPATH and I want to keep it together my express / go / docker files for this project.
I tried to clone iris framework's git repo into a ./vendor subfolder, but using import "github.com/kataras/iris" importing nothing.
Is there a package manager which is
copying and installing packages and all of it's dependencies in my-project/vendor folder outside $GOPATH
it can update these import packages
go run/build/install outside $GOPATH
there's now any new files in $GOPATH src/pkg/bin folder when I working on a project, except this package manager
I can define dependency packages for a project like package.json file for node.js
Is there a go package manager like that?
Edit:
Running this with go command is not required.
Not possible with the current go tooling, but looks like we might get it in go 1.12 or bit later.
Proposal accepted:
cmd/go: modify the Go toolchain to work without GOPATH

What is the idea behind Go package naming convention?

I'm trying to understand the idea behind package naming convention in Go. Most packages are installed and imported as something like:
import "github.com/howeyc/fsnotify"
I get the idea that package names should be unique, but I don't see the point of using the website github.com. Why not just use author/package? Like:
import "howeyc/fsnotify"
That's not likely to ever collide. Or some other "shorter" strategy? Is it because it "just works" with go get? Or is there some other reason?
You can use howeyc/fsnotify if you want to. When github.com/howeyc/fsnotify is used it's understood that the package is hosted on Github. Other repositories work as well.
The reason is it makes it easier to locate and install dependencies with go get. Otherwise you'd have to satisfy the dependencies manually. And since forking repos is quite common in the open-source world, you may have a modified version from the same author. So it helps to distinguish what your project depends on.
Download and install packages and dependencies
Usage:
go get [-d] [-fix] [-u] [build flags] [packages]
Get downloads and installs the packages named by the import paths,
along with their dependencies.
When checking out or updating a package, get looks for a branch or
tag that matches the locally installed version of Go. The most
important rule is that if the local installation is running version
"go1", get searches for a branch or tag named "go1". If no such
version exists it retrieves the most recent version of the package.
For more about specifying packages, see 'go help packages'.
For more about how 'go get' finds source code to download, see 'go help remote'.
The import path supports the go get command. Paths denoting remote repositories begin with the path to the code. Run the go help remote command for details.

Resources