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.
Related
I'm writing my first Go code which among other things, sends an email. After finding out that the package net/smtp only supports Plain Auth (but some providers like outlook doesn't support it), I asked for a solution and got pointed out to https://gist.github.com/andelf/5118732.
That code works like a charm, but as it's not something written by myself, I would like to add it in a separate file and just reference it in my main.go.
What's the right approach to have multiple files in the same package? I don't want to create a different package just for that code, first because it's not mine, and secondly, because I think it's an "overkill" approach, isn't it?
I thought that as long as the files are in the same directory, I could have many *.go files, but it seems it's not working out. If I just create a file with the content of that gist, the compiler fails because expected package, found import. If I add something like package auth, then it fails because found packages auth (auth.go) and main (main.go)
So, what's the general practice in this situations? Just create packages for everything?
You can have only one package in a directory, and it looks like you don't need a package for this addition, so you can simply put that in a separate file, and add package main at the top. Having a main package and putting everything under it works up to a point. As things get larger, you have to break it up into self-contained packages.
If your current working directory is in GOPATH, then you can just add new go file with same package name main.
If your current working directory is not in GOPATH, you can still put them in multiple go files and when you run the program, you should use go run *.go instead of just go run main.go.
There are also other options which you can refer Run code with multiple files in the same main package in GoLang for detail.
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'm trying to debug Go implementation of ethereum(link), because my core interest is in developing new consensus algorithm (i.e. modify the open source Go code from github).
However, I'm having problem with the location/path of the source code. When I put the folder(i.e. go-ethereum) outside of the $GOPATH and then try to compile&debug geth(go-ethereum/cmd/geth/main.go) it shows the following error: Use of internal package is not allowed.
From that error message, I figured out that the import github.com/ethereum/go-ethereum was not importing my source, and instead it was getting code from internet(like other libraries). Which of course is definitely what I shouldn't do when I'm trying to modify the github.com/ethereum/go-ethereum package code.
So, my workaround was to clone the source code into $GOPATH/src/github.com/ethereum/go-ethereum and followed this answer, and Goland IDE started compiling&debugging without error (wasn't able to go build ./cmd/geth/main.go though due to error undefined: configFileFlag...)
Thereby now I've got a working debugger that can debug with my source code modification, but this doesn't look like ideal source code structure.
The question is:
Is putting source code inside $GOPATH(because of internals) a proper approach? If so, what if I was using go-ethereum package from another project?(Fortunately I'm not, but I'm curious) Do I have to stash&revert changes I made to the code?
Yes, the folder structure you ended up with is the right one.
Code should be under $GOPATH/src as you describe.
But note that $GOPATH is not a fixed folder in your system, you can have multiple projects under different folders and change the value of $GOPATH accordingly depending what you are working on (or have multiple console terminals open, each with its own $GOPATH value).
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)
I need to use "configure --prefix" in order to "make install" in a particular location. Only a makefile is provided, not a configure file. I have never used autoconf or created a configure script myself. I found a guide here, but got lost at the 5th step involving target binaries etc. Is there any way to make install in the location I need it to be in without creating a configure file. If not, is there any easy way (or a simple guide/tutorial) to make a configure file?
Your question is not very clear to me. Are you trying to create a configure for a package you authored, or are you trying to install some (unnamed) package that does not follow the GNU Build System conventions? I'm assuming the latter.
If the package you are trying to install does not come with a configure file, there is no point in trying to run ./configure, and I would advice against building one unless you are very familiar with that package (and in that case you would know how to install it).
Without knowing what package it is, I would suggest the following course of actions:
Search this package's README files or web pages for instructions about installation.
Look into the Makefile. Maybe you will find a simple install rule and all you have to do is edit that rule or some variable to point to the desired location. Some package are so simple they don't even come with an installation rule: for instance they maybe build a binary, and you are expected to copy that binary wherever you want by your self.
Contact the author(s) of that package.