I'm currently developing a service that can be built as windows service or run as OSX/linux executable.
I'm using build tags on windows files, including the one with a main method
// +build windows
And on the other file containing a main method
// +build !windows
When I execute go run *.go on the mac side, I get the following error
mainDOS.go:10:2: no buildable Go source files in /Users/michaelbrandenburg/Documents/git-repo/goCode/src/golang.org/x/sys/windows/svc
windowsService.go:15:2: no buildable Go source files in /Users/michaelbrandenburg/Documents/git-repo/goCode/src/golang.org/x/sys/windows/svc/debug
install.go:14:2: no buildable Go source files in /Users/michaelbrandenburg/Documents/git-repo/goCode/src/golang.org/x/sys/windows/svc/eventlog
install.go:15:2: no buildable Go source files in /Users/michaelbrandenburg/Documents/git-repo/goCode/src/golang.org/x/sys/windows/svc/mgr
Is there a way to run go run and target the architecture I want to run? I can build the executables with no problem.
GOOS=darwin go run *.go will set the env for Mac OSX. Though, like JimB said, there isn't much of a point. Doing GOOS=darwin go build *.go is a good way to cross compile though
Related
For some reasons I need to use "go tool compile" to compile go files instead of using the automatic go build.
"go tool compile" works fine with one file or multiple files in the same package, but if my project structure is like:
-package1
--p1.go
-package2
--p2.go
main.go
go.mod
where main.go uses symbols defined in package1 and package2.
Then how am I supposed to use :go tool compile" to eventually get an executable program?
I am attempting to compile a go program. The code is a small x11 window manager, found HERE. (The code is 98 lines, too long to post here)
Here is the command I run, on x86-64 Ubuntu:
GOOS=linux GOARCH=386 go build littlewm.go
This command works fine on other files in the same directory. However, when I try to compile this one, I get the following error:
can't load package: package main: no buildable Go source files in /home/ubuntu/workspace/Go
This does not involve the gopath variable, as I have successfully compiled other programs in the same directory. I have a suspicion it involves the code itself or some option I am lacking. As such, this is not really a duplicate of an existing question and is quite possibly unique, since I am not getting the answer I want from other similar questions. Any help is appreciated greatly.
Thanks!
I encountered a similar problem when trying to build my own go program.
I had just added GCO to interface with some native C code in a library.
It built for me for the native platform (the machine I was coding on), but not when I tried to target another platform (using GOOS and GOARCH as you're doing).
Setting CGO_ENABLED to 1 (as mentioned in david's comment) fixed the problem for me:
CGO_ENABLED=1 GOOS=linux GOARCH=386 go build littlewm.go
I was using a makefile to build for multiple platforms using multiple rules, so for me, I put
export CGO_ENABLED = 1
near the top of my makefile so that I didn't need to specify it for each platform.
I am writing a go program to parse csv files and upload them to a postgres database in the Heroku cloud. The program works fine on my mac laptop. Eventual target platform for deploying the code is Windows though. I did some research and found that the following command creates a Windows executable on my Mac.
env GOOS=windows GOARCH=386 go build -v main.go
This produced an executable called main.exe when I had just one file called main.go. The program has grown since then and I have it now split into three source files:
main.go
common.go
specific.go
They are all in the same source directory though. Moreover, since the program is relatively small, I have all functions in the 'main' package. What is the command now to cross-compile on mac for windows for multiple source files belonging to the same package? The previous command did not work since the other two file common.go and specific.go are not included. Also, I would like to know how to rename the Windows 'exe' file to something else than main.exe, e.g., upload.exe? I am using go version go1.5 darwin/amd64
How can I determine if a package will build, without installing it, running its tests, or generating a binary?
There's a mention that using go build with more than one package just tests if they build. How can I do that for a single package?
go build
For package main, a binary will be generated by go build. You can dump that in a tmp directory that the OS will clean for you later.
go build -o /tmp/oswilldeleteme
Unfortunately, you can not pipe the output of go build to the null device. See this issue: https://github.com/golang/go/issues/4851
After writing some scripts in Go I asked myself if there is any difference between the compilation of a .go-file and the later execution and the go run FILE.go command in terms of performence etc.
Are there any advantages if I start a webservice with one of these methods?
go run is just a shortcut for compiling then running in a single step. While it is useful for development you should generally build it and run the binary directly when using it in production.
'go install' command will create shared library compiled file as package.a under pkg folder and exec file under bin directory.
go run command is useful while doing development as it just compiles and run it for you but won't produce binaries in pkg folder and src folder
For DEV (local) environment - use go run,
For PROD environment - use go install this one better than go build because it installs packages and dependencies and you'll have Go toolchain.