To compile a plugin in go I need to run the command below.
go build -buildmode=plugin
Is it possible to call some internal API instead of the command-line option to do this? I can always package the go binary and call os.exec("") but I want to avoid that if I can.
Since go is build with go, the go/internal (https://pkg.go.dev/std see internal) place is where you want to start looking. It is a rabbit hole but contains all the items like environment variables (GOOS), build config, go root, etc.
Other projects building some code for immediate usage (like skaffold) seem to have opted for os.Exec
Related
My project consists of multiple go modules which I need to work on simultaneously. This is made easy by using the replace directive in the go.mod file.
Further, in order to prevent this change from getting accidentally checked in, the go tools (starting with version 1.14) offer a -modfile switch which lets me put the replace directive in a go.local.mod file instead. This is super convenient.
Unfortunately, I am unable to get Goland to pick up this go.local.mod instead of go.mod.
I have tried setting the GOMOD environment variable under Preferences -> Go -> Go Modules to point to my go.local.mod file. This did not do what I expected it to (which is to use the go.local.mod file instead of go.mod)
Not surprisingly, this did not work since GOMOD is supposed to be readonly (as pointed out to me in the comments).
For now I can go back to putting the replace directive in the go.mod file and using commit hooks to prevent accidental check-in (and Goland has some tooling to perform this check as well) However, it would be super convenient if Goland can recognize this new -modfile switch that go has added. Figured I would ask to make sure I'm not missing something obvious here.
Thanks!
At the moment, 2020.1.3 stable release, this is not supported. See the related issue for future updates on this.
I"m trying to contribute for a project and the docs tell me to use this command
go get github.com/foo/bar
but the error is
can't load package: package github.com/foo/bar: no Go files in /home/f/go/src/github.com/foo/bar
Obviously its looking on my computer but how do I make it so that it downloads from the web?
The problem is that the project you're trying to download cannot be built, because Go can't find any source files to build at the source path github.com/foo/bar. The package is, however, downloaded, and if you look in $GOPATH/src/github.com/foo/bar you will see the repository cloned there. So if you want just that, then you're done, but you can use go get -d in the future to avoid the error message.
If you want something specific that can be imported, e.g. github.com/foo/bar/somepackage, then you should use go get github.com/foo/bar/somepackage.
I can specify dependencies to be downloaded by go get after checking out my project by importing them. I can even force the download of packages that are not used in the code by importing them for side effects:
import _ "github.com/jteeuwen/go-bindata"
Furthermore, on the shell I can apparently install a program with go get by using an ellipsis after the path:
$ go get github.com/jteeuwen/go-bindata/...
However, it seems I cannot combine both techniques:
import _ "github.com/jteeuwen/go-bindata/..."
$ go get
main.go:9:8: open c:\gopath\src\github.com\jteeuwen\go-bindata\...: Access denied
I would like to tell go get that for building (actually go generateing) this project, go-bindata has to be installed. Is it possible to specify install-dependencies?
To answer your question: No.
But you could vendor go-bindata into your project which would make it available after after go geting your project.
But maybe there is a slight confusion about when and why to run go generate: The intended use for go generate (as I understand it) is for package or command authors to generate code during the development phase. Code which is checked in and processed "normally" by go {build,install,get}. So you run go generate, check in the generated stuff and users of your package go get it and do not run go generate. They don't need to, the code comes in the proper checked in version during geting.
For more complicated builds which a end-user has to perform: Use Makefiles or similar tools as such stuff is out of the scope of go get.
In one of my projects I use Godep. According to its page:
This tool assumes you are working in a standard Go workspace, as
described in http://golang.org/doc/code.html. We expect godep to build
on Go 1.4* or newer, but you can use it on any project that works with
Go 1 or newer.
You'll have your dependencies in a JSON file, just like Node, Bower, etc... It's vert simple to use.
In your case, assuming you already have go geted the package, run:
godep save
This will generate the JSON file with all your other dependencies and save to a folder in your project. Thanks to it I was capable of cross compiling my project.
Using Go on Windows, whenever I execute go run myprog.go from the console, Go builds a new executable with a random name somewhere on my C drive.
Is there a way to configure it so it will always build the file to a specific location, and preferably to also avoid the randomness of the name? (i.e., always build to D:\Temp\LastBuild.exe).
I was able to find some info that did not help when doing go help run and go help build. The latter had an output flag -o outfile but it is not accepted in go run.
Any help is appreciated.
Do not use go run. It's intended for quick testing out snippets of code the size of a single screenful of lines.
The working approach is
Edit the code.
go build it.
Run your executable which will have predictable name.
Go to step (1).
If you write tests in parallel with the implementation (and you should), this changes to
Edit the code.
Edit the test suite.
go test it.
Go to step (1).
Please see this recent thread on the mailing list for more insight on the same subject.
I'm developing a small tool with Go. And recently, I noticed that the tool needs to be invoked from a shell script, because it's using shell function.
Assume my tool is called atool. So, go build generates a binary atool, and my tool has a Go structure as github.com/myaccount/atool. Now, i want to build atool-cli binary with go build, and invoke it from shell script atool. How can I achieve this?
The only way coming in my mind is change go structure as github.com/myaccuont/atool-cli. But I don't want to do this because the already announced, and also, the path seems a bit funny name.
Just to make my comment "official":
go build -o atool-cli github.com/you/atool
One way packages structure themselves as a library, and provide main packages is to put their main entrypoints in subdirectories.
You can have a main package in github.com/myaccount/atool/atool-cli, which imports github.com/myaccount/atool and implements func main(). Some packages with multiple commands even have a /cmd/ directory with multiple cli tools that can be built (see camlistore as an example)