My go application directory structure is like this:
/app
go.mod
go.sum
main.go
When I build the app I usually cd into that directory and build.
cd app
go build
I wonder if I can build without cd in to app directory.
When I go go build /app, it prints go: go.mod file not found in current directory or any parent directory; see 'go help modules'.
See https://golang.org/ref/mod#commands-outside :
go build needs to be run from a module directory.
The simplest way is to cd into your module directory (cd /app) to run your go build command.
(there probably is some way to create a phony local go.mod file, and reference your /app module from there, but I wasn't able to devise a hack to do this)
Go now can change directory before build with the help of flag -C:
go build -C app
"The go subcommands now accept -C to change directory to before performing the command"
Source: https://tip.golang.org/doc/go1.20
(You don't need to run cd .. after this command. Shell stays in the same directory)
I'm pretty sure to build you will need to be in the app directory.
As a workaround if you are just wanting to be on the command line in a different directory and want to run the build with one command you can just chain the cd and go build commands together like this:
cd app; go build ; cd ..
This is the same amount of typing but could be in your command history just a couple presses of the up arrow away.
Note this is for bash or similar UNIX style shell. If using Windows cmd then I think it would be something like this (Not tested as I don’t have readily available access to a Windows machine right now):
cd app & go build & cd ..
Related
I'm looking to run the main.launch stored in vehicle/launch/ from this github page
https://github.com/aureliopuebla/vehicle
I am very new at using ROS and have been learning, however I can't seem to be able to build these files.
If I try to use catkin_make on the parent folder it says that there is no existing 'src' folder.
If I go into the /vehicle folder there is a 'src' folder, but if I try to run catkin_make there, then it says that I have to run it at the root of the workspace. Which has me a bit stumped.
I have also tried to just run 'cmake ..', then 'make', and then 'sudo make install' in the /vehicle folder, but that just fills the /vehicle folder with copies of the other folders in the parent folder.
The reason why I want to build these packages is to be able to run the 'main.launch' file inside the '/vehicle/launch' folder with roslaunch, but it keeps saying that it can't find the other packages, no matter what I do.
Ready to clear up any questions. Thanks for the help.
the CMakeLists.txt in the folder is the top-level CMakeLists. So You need to make this src folder yourself.
Just do the following:
$ mkdir -p vehicle_ws && cd vehicle_ws
$ git clone https://github.com/aureliopuebla/vehicle.git
$ mv vehicle src
$ catkin_make
This way it should work. Just leave out mkdir -p vehicle_ws, if you already created a workspace and instead just cd into it.
Right now I'll just run go build __.go, but I'm wondering if it's possible to have that file built in a subdirectory (e.g. a /bin folder). It would just make gitignoring the binary files a lot cleaner, and right now I'm not really sure what else is a good approach as I'm also struggling to create a working gitignore exception rule that isn't just "Ignore all files, except .go files".
My current solution is naming the binary files every time I build them (e.g. go build -o hello.bin hello.go), but this seems laborious.
you can use the -o flag.
cd to the directory where you have the main.go file.
use this command - go build -o DirectoryPath
Where DirectoryPath is the location where you want to create the binary file to.
Instead of using go build *.go, you can do following:
Setup Go workspace, using this official go resource: How to write go code
Define GOBIN and GOPATH environment variable.GOBIN environment variable will point to the directory where you want to store your executables.GOPATH environment variable will point to the directory, where you've setup go workspace.
Now, instead of doing go build *.go, you can use go install {YourCodePath}, where {YourCodePath}, is relative path of your code in go workspace. And if the build is successful, you can find your executable in directory, pointed by GOBIN.
I trying to deploy my server on heroku and I stuck on step where I should use godep, I spent little time with GO, last two days I had looking how to solve this issue, actually it's a popular issue, but I can't figure out, maybe I did something fundamentally wrong.
I have OS X 10.11.2
My GOPATH - Users/denis/Programming/Golang
My PATH - $GOPATH/bin
I trying to use godep to my project which is placed in $GOPATH/src/backend
in my PATH I have an executable godep file(not .go).
Whole structure of my workplace.
/Golang/
.bin/
godep //executable file
.pkg/
.darwin-amd64/
.//other folders/
.src/
.github.com/
.backend/
.//other folders//
First, what is the output of this:
$ echo $PATH
It should, at a minimal to run Go projects, have TWO directories in it for:
/path/to/go/installation/bin (e.g. /usr/local/go/bin)
/path/to/your/GOPATH/bin (e.g. /Users/denis/Programming/Golang/bin)
Your go's installation should be in your PATH already if you followed the Go installation setup.
https://golang.org/doc/install
Since you posted /Users/denis/Programming/Golang, with the capital U, I am going to assume that you are on OS X going forward...
In OS X, and if you used the default install, you can test for Go in your PATH with a simple command:
$ echo $PATH | grep --color=auto "/usr/local/go/bin"
It should print our your entire $PATH and highlight that you have things setup properly. This is because the OS X GoLang installer in the URL above should have modified your PATH to include:
/usr/local/go/bin
or more specifically, it may be export PATH="$PATH:/usr/local/go/bin"
If you have some custom .bashrc and/or .profile files, then most likely your PATH isn't being setup correctly. You can test this by doing this:
$ export PATH="$PATH:/usr/local/go/bin"
$ export PATH="$PATH:/Users/denis/Programming/Golang/bin"
$ godep go build
If it works now, then your PATH isn't setup properly in your .bashrc/.profile files. That's a different kind of question and can be setup a 1000 different ways and you may need to figure it out (or another SO question).
If that resolves your issue, then you need to follow the godep directions to run godep from your project's root:
$ cd $GOPATH/src/backend/
$ godep save
$ git add Godeps/
godep creates a Godeps directory in the root of the Go project. If you have multiple Go projects/multiple executables, then you need to run godep save on each root of the runtime.
IOW, for each executable you run go build for, you need to run godep save in each directory.
Also, once you move to godep, you want to use it for your build and testing as well:
$ godep go build
$ godep go test
...etc
If that's not the problem (you are running it as stated above), then please update your question with more specifics such as what godep command you are running, where, and what that directorys structure looks like for the root of that project (including the filename with package main and your func main() function).
I'm at the point where want to organize my Go web server into packages. Currently I have everything in a few files and I simply type: 'go run server.go foo.go bar.go'
How do I organize my files so I don't need to keep adding files to the command line. I've investigated the GOPATH variable but it doesn't seem to work.
export GOPATH=$HOME/myserver
I moved my files to the src/ subdirectory.
myserver/src/server.go
myserver/src/foo.go
myserver/src/bar.go
Shouldn't 'go run' search $HOME/myserver/src for all go files?
I've tried these examples but they don't work.
go run server.go; # Doesn't work
go run src/server.go; # Doesn't work
By the way, all files are in 'package main'
This info is covered really well on golang.org
Read this about how to write Go code
And this about organizing go code
Tip: you can run go run * to run all files in a folder
Your above example should look something like this
$GOPATH=$HOME
The GOPATH should have:
$GOPATH
src/
pkg/
bin/
$GOPATH/src is where you would store your source code for each go project
$GOPATH/src/myserver would contain your myserver program
cd to $GOPATH/src/myserver and run go install and you would now have your myserver binary located at $GOPATH/bin/myserver
Add the location of your bin to your path export PATH=$PATH:$GOPATH/bin and you can run myserver to start your go program
go run will look for a file (or wildcard) in your current working directory.
If you would like to run your programs from anywhere, either use go build same as you would go run and move the binaries where appropriate or, better yet, set your $GOBIN environment variable and add it your $PATH -- then run go install * in your project directory.
It's also probably a good idea to make specific directories for projects instead just dumping it all in $GOPATH/src
I am using xcodebuild from command line in a script, but I realized that I cannot specify the path of the project that i wanna build;I am forced to cd in the folder where the project is.
Is there a way to accomplish the build process without having to cd in the directory, or this is how it must be?
Is not a big deal to cd into the directory and execute xcodebuild, but I wonder what if someday you need to build a project and you cannot cd into the directory....It doesn't really make sense to me to not being able to specify the path.
You can use xcodebuild -project pathtoprojectfile
eg
xcodebuild -project /IOSprojects/YourProject/YourProject.xcodeproj
You must be in the directory containing the project(s) when you run xcodebuild. If you don’t want to mess with your current directory, there a couple of options:
/bin/sh -c "cd $PRJDIR; xcodebuild"
or
(cd $PRJDIR; xcodebuild)