can't load package: package .: no buildable Go source files - go

Here is the error message:
% go get
can't load package: package .: no buildable Go source files in /Users/7yan00
% echo $GOPATH
/Users/7yan00/Golang
How would you troubleshoot that error?

Make sure you are using that command in the Go project source folder (like /Users/7yan00/Golang/src/myProject).
One alternative (similar to this bug) is to use the -d option (see go get command)
go get -d
The -d flag instructs get to stop after downloading the packages; that is, it instructs get not to install the packages.
See if that helps in your case.
But more generally, as described in this thread:
go get is for package(s), not for repositories.
so if you want a specific package, say, go.text/encoding, then use
go get code.google.com/p/go.text/encoding
if you want all packages in that repository, use ... to signify that:
go get code.google.com/p/go.text/...

You should check the $GOPATH directory. If there is an empty directory of the package name, go get doesn't download the package from the repository.
For example, If I want to get the github.com/googollee/go-socket.io package from it's github repository, and there is already an empty directory github.com/googollee/go-socket.io in the $GOPATH, go get doesn't download the package and then complains that there is no buildable Go source file in the directory. Delete any empty directory first of all.

Another possible reason for the message:
can't load package: .... : no buildable Go source files
Is when the source files being compiled have:
// +build ignore
In which case the files are ignored and not buildable as requested.This behaviour is documented at https://golang.org/pkg/go/build/

To resolve this for my situation:
I had to specify a more specific sub-package to install.
Wrong:
go get github.com/garyburd/redigo
Correct:
go get github.com/garyburd/redigo/redis

If you want all packages in that repository, use ... to signify that, like:
go get code.google.com/p/go.text/...

you can try to download packages from mod
go get -v all

I had this exact error code and after checking my repository discovered that there were no go files but actually just more directories. So it was more of a red herring than an error for me.
I would recommend doing
go env
and making sure that everything is as it should be, check your environment variables in your OS and check to make sure your shell (bash or w/e ) isn't compromising it via something like a .bash_profile or .bashrc file. good luck.

Related

Trouble with using gorilla/mux package in mac

I am trying to learn how to build a webserver using go and mux. I am importing mux to the main.go file as import github.com/gorilla/mux. However, when I am trying to run the code. I get the following error
no required module provides package github.com/gorilla/mux: go.mod file not found in current directory or any parent directory; see 'go help modules'
My GOPATH is /Users/michiokaku/Study/go
The overall structure of my directories is
go___
pkg
bin
my_codes___
main.go
Inside pkg, I found a directory named mux#v1.8.0 in the path pkg/mod/github.com/gorilla. I think this is what I downloaded using go get -u github.com/gorilla/mux. But when the code is running, I am getting errors.
What is the issue here? How do I solve this?
PS: I am using mac.
Read through Tutorial: Getting Started with Go, if you haven't seen it already. It matches your situation pretty closely.
In short:
Run go mod init example.com/projectname, replacing the last argument with the name for your module. This will create a go.mod file in the current directory that will track your dependencies. Your module's name will be a prefix for all packages within your module.
Run go mod tidy or go get github.com/gorilla/mux to add github.com/gorilla/mux as a dependency.
You mentioned you saw a directory pkg/mod/github.com/gorilla earlier. This is part of Go's module cache, shared by all projects.

How to find a Go package installed with 'go get -u' when no $GOPATH is defined?

I'm following the gRPC Quickstart tutorial for Go, https://grpc.io/docs/quickstart/go/, and have installed gRPC using the command
go get -u google.golang.org/grpc
I actually haven't defined a GOPATH environment variable:
> echo $GOPATH
which, as I understand it, means that it defaults to ~/go, or in my case /Users/kurt/go.
At the next step, I'd like to build the example by doing
cd $GOPATH/src/google.golang.org/grpc/examples/helloworld
However, I find that the directory doesn't exist, and there is also no google.golang.org directory in /Users/kurt/go/src:
~/g/src> ls *google*
fish: No matches for wildcard '*google*'. See `help expand`.
ls *google*
^
Should the package not be located here? That's what I understand from Where does go get install packages?.
Using Go Modules, you can find 'go get' downloaded files at:
~/go/pkg/mod/cache/download
However, it should be treated like an immutable copy of the source code.
If you want a mutable copy of the source code, you should clone the repository:
git clone https://github.com/grpc/grpc-go
In your example output, you are in ~/g/arc
Go path default would be ~/go/src
I think auto complete bit ya there
In the end, I worked around the problem by cloning https://github.com/grpc/grpc-go which appears to contain the examples/helloworld directory I'm looking for. Still curious to hear where the package downloaded with go get is located.

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

"go get -t ./...", Travis CI, and installing all go dependencies

In the documentation here: https://docs.travis-ci.com/user/languages/go#Dependency-Management
This is the install dependencies step:
if go version is greater than or equal to 1.2
go get -t ./...
My project looks like this:
root
---- src
---- github.com
---- myProject
---- pkg
the GOPATH is set to root
but I am getting this error:
package github.com/aws/aws-lambda-go/lambda: home/travis/build/path/to/package exists but home/travis/build/path/to/package.git does not - stale checkout?
How do I get all the dependencies to install at once like an npm install in nodejs?
How do I get all the dependencies to install at once?
That's what go get does... it looks like something might be misconfigured or corrupted in your case. You could start by trying some things.
go get installs into the first path in the $GOPATH environment variable. Confirm that it is set to what you want (usually a path ending in a directory named go; subdirs src, pkg, etc. will be created).
If you're going to use ./..., make sure you're calling go get from the right directory.
Check to make sure the git root is in the right place
try using a non-wildcard name/path/address for the package you want to get, instead of ./...
try calling go get without the -t flag
If none of that works, you may be able to solve the problem by deleting the directory (home/travis/build/path/to/package) and trying again -- make sure you aren't deleting any code you worked on, or the git repository/files, unless it's backed up somewhere.
According to Go tools documentation, you should only need to call
go get [packages]
to install the named packages along with their dependencies:
Get downloads the packages named by the import paths, along with their dependencies. It then installs the named packages, like 'go install'.
The ... ellipsis is a wildcard that can expand to match any string. See section on Description of Package Lists:
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.
The ./ means "here": so make sure you're running from the right directory if you're going to use ./...
The -t flag is for downloading the packages required to build the tests:
The -t flag instructs get to also download the packages required to build the tests for the specified packages.
The error you're seeing is related to git. Sometimes the cause is unknown, but it can often be fixed by deleting the directory and starting again (see, e.g., "Error to install golint" or "Correct way to get package")
(You might also find this blog post on configuring travis-ci for Go helpful.)

How to build Golang blog from github?

Im not very good with Go and I am having a lot of problems with understanding how common website features are made, so I thought it would be good to see a real example. I tried building https://github.com/golang/blog but its not working.
My gopath is apparently C:/Users/me/go as it should be.
*Edit Except if I run cd $GOPATH/src, it says C:\src doesnt exist, it looks in C: not C:/Users
Method 1. (running go get -u golang.org/x/blog)
I open Powershell and run that in my Users/me/go/src directory and it says:
can't load package: package golang.org: no Go files in
C:\Users\me\go\src\golang.org
But it does download the source files. So its basically this step?
'u can manually git clone the repository to $GOPATH/src/golang.org/x/blog.'
Then I dont know where to run go build or what to run. I tried
go build -o blog.exe ./blog
and it says
can't load package: package blog: cannot find package "blog" in any of:
C:\Go\src\blog (from $GOROOT)
C:\Users\me\go\src\blog (from $GOPATH)
I tried running the same command in different directories of the project and doesnt work.
I'll try to answer your questions. (Note that I am a Linux user, so there may be some discrepancies with the Windows commands below. You may want to follow these directions: http://www.wadewegner.com/2014/12/easy-go-programming-setup-for-windows/ to setup the GOROOT environment variable.)
For method 1, the -u flag tells go to update the source code. Since you haven't downloaded it before, it lets you know with the error you see. What you want to run is go get golang.org/x/blog.
To build the package, you first want to change the directory (cd) to the package root, so cd %GOPATH%\src\golang.org\x\blog or cd C:\Users\me\go\src\golang.org\x\blog. Next, you want to run go build. Then, you can run the output file, which should automatically be named blog.exe.
Hopefully this helps! :)

Resources