How to build after "go get -d"? - go

I'm using the GOfax.IP which I can download and build with:
go get github.com/gonicus/gofaxip/...
This works without any problems.
Now I want to make a small change in the source code before compiling.
I know that I can use go get -d to only download the sources, but how would I start the build/compile part of got get after I finished my modifications?
Thanks.

I found the answer myself:
go install ...
This seems to do exactly the same as go get after downloading the sources.

go -h can help you:
Go is a tool for managing Go source code.
Usage:
go command [arguments]
The commands are:
build compile packages and dependencies
...

Related

Incremental builds not working with go build, only with go install

I'm running a project using Go Modules with 1.11.4 on Ubuntu, running in WSL.
My problem is that I'm having getting incremental builds to work as I expect. Perhaps this is due to me misunderstanding how it's supposed to, but I'd be glad if someone could clarify if that is the case.
Just as an example, if I do go build ./... then everything is built, as expected.
If I now do go build ./... again without any changes, my expectation was that due to the incremental builds, this time nothing would be built. But it builds everything again. I tried doing go build -i ./... (even though my understanding is that -i isn't needed anymore from 1.10), but the result is the same. This has been puzzling me for some time, as after reading the documentation I indeed expected the go build command to produce incremental builds.
The other day I realized that if instead I do go install ./... first, and then go install ./... again a second time, the second time around nothing is built, as I would expect. If I change a single module and run go install ./... again, then only that module is rebuilt, again what I would expect. So this gives me incremental builds.
So my questions are
1) Did I misunderstand go build ./... and how it handles incremental builds? Do I need to use go install instead?
2) Typically, we build the modules one by one, using the -o flag to specify an output path. Using go install instead, there is no -o option to specify an output path. Is there anything I can do to achieve something similar to -o using go install?
Thanks!

Can't build or install GO tools binaries

I am trying to build some GO tools in my project. I first run
go get golang.org/x/tools/benchmark/parse
The folder/binary structure does appear correctly under
$GOPATH/src/golang.org/x/tools/benchmark/parse
I tried running: go build golang.org/x/tools/benchmark/parse
and
go install golang.org/x/tools/benchmark/parse
however the binaries still do not appear in my $GOPATH/bin
Any help is greatly appreciated!
You can't build benchmark/parse, but you can import it.
From Godoc for Tools:
Package parse provides support for parsing benchmark results as generated by 'go test -bench'.
sudorandom's comment is right, parse.go doesn't use package main, so it won't generate a binary, while you can use it within your own code with import "golang.org/x/tools/benchmark/parse".

Specify Go build flag "-H=windowsgui" in code comment

I am creating a desktop Windows application in Go. Currently I use this line to install it:
go install -ldflags -H=windowsgui
I want users to be able to just say
go get github.com/my/app
and automatically have the windowsgui flag specified for building. Is it possible to add the flag as a code comment, like #cgo comments. Or would I have to provide a make file in the project directory like in the old days of Go? Or is it not possible at all?
Bad news, you can't.
Semi-good news, you can tell your users to use go get -ldflags "-H windowsgui" github.com/my/app on Windows.

How to package go project for homebrew

We're developing runscripts and try to support something like brew install runscripts.
It's written in golang and have some dependencies which required to go get. Now I have no idea to write the Formula to setup GOPATH and run go get. Our project can be compiled into an binary but we need run --init to install it.
Can anyone helps to give an example about a homebrew Formula of go project?
Fork homebrew, look at the content of Library/Formula/consul.rb. You don't need to manually generate all the resources. Use homebrew-go-resources. A more complete intro could be found here.
I have refer to termshare.rb and it seems we can simply go get and homebrew will handle anything about GOPATH for us.
That's great and I think my problem is solved.
The best I could find is how docker-swarm is added to brew: https://github.com/Homebrew/homebrew/blob/4c6857b0e337b2d5afd49dcf7209b6b5091709f4/Library/Formula/docker-swarm.rb
It's relatively clean and simple to follow.
You can use goreleaser to generate the formula automatically: https://goreleaser.com/customization/homebrew/
Here is a possible work around creating a build directory within the checkout as the GOPATH:
...
def install
system "mkdir -p build/src"
system "ln -s `pwd` build/src/repo"
system "GOPATH=`pwd`/build go get repo/mytool"
bin.install "build/bin/mytool"
...
end
...

"go build" became very slow after installing a new version of Go

After upgrading from Go 1.2.1 to 1.3 (Windows 7 64 bit) "go build" execution time has increased from around 4 to over 45 seconds. There were no other changes except the go version update. Switching off the virus scanner seems to have no effect. Any clues?
You probably have dependencies that are being recompiled each time. Try go install -a mypackage to rebuild all dependencies.
Removing $GOPATH/pkg also helps to ensure you don't have old object files around.
Building with the -x flag will show you if the toolchain is finding incompatible versions.
I have the exact same problem, running this command solves it:
go get -u -v github.com/mattn/go-sqlite3
Another tip: http://kokizzu.blogspot.co.id/2016/06/solution-for-golang-slow-compile.html
Using go1.6,
Simply run go build -i.
It will compile all the dependencies and store them at $GOPATH/pkg/*/* as .a files.
Later when you run go run main.go, everything is much faster.
What s really great is that if you use vendored dependencies (IE: a vendor folder in your project), deps are built appropriately within $GOPATH/pkg/**/yourproject/vendor/**
So you don t have to go get install/get/whatever and have a mix of vendor / global dependencies.
I suspect you got to re-build .a files after deps update (glide update or smthg like this), but i did not test that yet.
After Go 1.10, you'd just need to type go build. You'd not need to type: go build -i.
From the draft Go 1.10 document, here.
Build & Install
The go build command now detects out-of-date packages purely based on the content of source files, specified build flags, and metadata stored in the compiled packages. Modification times are no longer consulted or relevant. The old advice to add -a to force a rebuild in cases where the modification times were misleading for one reason or another (for example, changes in build flags) is no longer necessary: builds now always detect when packages must be rebuilt. (If you observe otherwise, please file a bug.)
...
The go build command now maintains a cache of recently built packages, separate from the installed packages in $GOROOT/pkg or $GOPATH/pkg. The effect of the cache should be to speed builds that do not explicitly install packages or when switching between different copies of source code (for example, when changing back and forth between different branches in a version control system). The old advice to add the -i flag for speed, as in go build -i or go test -i, is no longer necessary: builds run just as fast without -i. For more details, see go help cache.
I just experienced the same problem - updating from 1.4 to 1.5. It seems that the olds versions are somehow incompatible or are being rebuild every time as go build -x shows. Executing go get -v invalidates all packages or refetches them, I am not quite sure and go build -x shows quite less output.
You can build sqlite3 like this:
cd ./vendor/github.com/mattn/go-sqlite3/
go install
After that your project will b built much faster.
If you try as all other said but still not work, I suggest removing the directory of $GOPATH such as :
sudo rm -rf $GOPATH
cd yourproject
go get -d
go get -u -v github.com/mattn/go-sqlite3

Resources