I am new to SO and a relatively new beginner to GO. I was building and running GO code smoothly on my local server until I had to use some of the packages from Gorilla toolkit.
What I ran in the command line:
mkdir $HOME/mygo
export GOPATH=$HOME/mygo
cd mygo
go get github.com/gorilla/mux
I did this based on the example from here: http://golang.org/doc/articles/go_command.html. Under gettng started with the Go Command and here http://www.gorillatoolkit.org/.
After this, I imported my main.go code into the mygo folder. My directory folder looks like this:
Workspace
pkg
src
main.go
However, when I ran go build main.go, and tried to run ./main, I got
-bash: ./main.go: Permission denied
What is happening?? Please help. Thanks!
First I highly recommend reading http://linuxcommand.org/learning_the_shell.php, then going through the Go tour.
Second, like #JimB mentioned, you can't execute main.go directly like that (you can use binfmt_misc but I'm not gonna explain that here).
So the steps to run your executable are:
go build -o main
./main
Or directly:
go run main.go
Related
I'm trying to install my package using go install but I get this error message when running the command go install github.com/JoaoDanielRufino/gcloc/cmd/gcloc#latest:
go install: github.com/JoaoDanielRufino/gcloc/cmd/gcloc#latest: module github.com/JoaoDanielRufino/gcloc#latest found (v1.0.0), but does not contain package github.com/JoaoDanielRufino/gcloc/cmd/gcloc
I want the executable name to be gcloc.
Here is the current source code: https://github.com/JoaoDanielRufino/gcloc
Note: I've already tried go clean -modcache but it didn't work
As the main function of this package isn't on its root, you should pass the directory of the main package on your command.
So, your command will be:
go install -v github.com/JoaoDanielRufino/gcloc/cmd#latest
I came across a similar issue when I was trying to use go install to install the cloudflare/cf-terraforming tool on my machine. The documentation for this tool is not clear on the installation and I had to dig around to get this to work
Basically #Jictyvoo answer above sums it up, if the path is pointing to anything other than directory where the main.go file is sitting I got the error
Command: go install github.com/cloudflare/cf-terraforming#latest v0.8.0#latest
go: github.com/cloudflare/cf-terraforming#latest: module
github.com/cloudflare/cf-terraforming#latest found (v0.8.0), but does not
contain package github.com/cloudflare/cf-terraforming
when I switched to the below it worked fine for me:
Command: go install -v github.com/cloudflare/cf-terraforming/cmd/cf-terraforming#latest
This worked for me after checking the repo and realising that the main.go file was sitting in the cmd/cf-terraforming subdirectory
After installing golang according to the instructions on the golang website, I set the path as it said to by using export PATH=$PATH:/usr/local/go/bin and whenever I use the command $HOME/go it says bash: /home/ken/go: No such file or directory
I can verify go is installed and I even reset my computer to make sure the changes took place. Does anybody know what I can do to resolve this issue? Am I missing something?
As per the instructions on the Go installation page, you do need to manually create the go folder:
cd $HOME
mkdir go
cd go
Then create your first go program: https://gobyexample.com/hello-world
And run with go run hello-world.go. Should be good ;)
I'm writing this away from my code so fingers crossed.
I've recently started learning Go from a Python background. I've set up my workspace (Linux Mint OS) so:
GOPATH=$HOME/go
GOROOT=/usr/local/go
Where under $HOME i have a dir called go and 3 subdirs called src, bin and pkg.
I wanted to mess around with some dataframes (I use pandas a lot in Python) so I tried to install gota from github. Only their recommended install command:
go get -u github.com/kniren/gota/dataframe
go get -u github.com/kniren/gota/series
returns an error saying it could find the package in GOPATH or GOROOT. To me this is strange as go get seems like the equivalent to pip install and shouldn't be looking in my path but rather at the URL. I managed to get some files to install but using goget and the github URL of the project:
go get github.com/go-gota/gota/dataframe
go get github.com/go-gota/gota/series
and this built some files under a gonum.org directory in my src dir and a linux_amd64 dir in my pkg directory. So far neither section have the expected path to the libraries and I can't find a suitable method to import. import statements look in gopath's src directory however I assume it should be looking in the pkg directory? Why is this and what's wrong with my env?
The authors of the repository must have migrated to a different repository.
The official repository of these packages is: github.com/go-gota/gota
https://www.github.com/kniren/gota/dataframe
https://www.github.com/kniren/gota/series
These repositories do not exist, this is why your go get commands failed. In fact, trying to navigate to github.com/kniren/gota redirects me to their official repository.
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! :)
I have a single file in the main package called main.go. Because the code isn't reusable I want to separate part of the code in a different file but in the same package.
How do I split the contents of main.go into multiple files without creating a separate package?
I want a directory structure like this:
ls foo
# output:
main.go
bar.go
File: bar.go
package main
import "fmt"
func Bar() {
fmt.Println("Bar")
}
File: main.go
package main
func main() {
Bar()
}
When I run go run main.go, it gives me:
# command-line-arguments
./main.go:4:2: undefined: Bar
Update 26th July 2019 (for go >=1.11)
go run .
Will work on windows as well.
Original answer (for non windows environments)
The code actually works. The problem was that instead of running go run main.go I should run:
go run *.go
Update August 2018, with Go 1.11, a section "Run" states:
The go run command now allows a single import path, a directory name or a pattern matching a single package.
This allows go run pkg or go run dir, most importantly go run .
Original answer Jan. 2015
As mentioned in "How to compile Go program consisting of multiple files?", go run expects a list of files, since it "compiles and runs the main package comprising the named Go source files".
So you certainly can split your main package in several files with go run.
That differs from go build/go install which expect package names (and not go filenames).
A simple go build would produce an executable named after the parent folder.
Note that, as illustrated by this thread, a go run *.go wouldn't work in a Windows CMD session, since the shell doesn't do wildcard expansion.
In my opinion, the best answer to this question is hidden in the comments to the top answer.
Just run this:
go run .
This will run all the files in main package, but will not give an error message like
go run: cannot run *_test.go files (main_test.go)
Kudos to #BarthesSimpson
As mentioned, you can say go run *.go but for Windows you can just list the script files (since *.go won't work) - go run main.go other.go third.go
The first method to do so will be to run
go run *.go
The another method is to generate an exe file
go build
Then run that .exe file
./filename.exe
Multiple options
go run .
go run *.go
make run using Makefile where, add any of the above command as build target.
for testing
go test ./...
make test using Makefile with go test ./... as build target
For Windows install Cygwin and use it instead of command prompt. "go run *.go" will work then.
If you are trying to run multiple files on localhost using gorilla mux in go as per latest version(1.11). Try using any of the following 2 commands.
go install && FolderName -port 8081 .
go build && ./FolderName -port 8081.
Make sure that you are in the source folder ie go/src/FolderName before executing the command in the Terminal.