Hey guys, I'm just struggling to get my head around something with go, I'm only looking at the language for a day or two.
I've read / been told that theres no runtime, and that go programs are single executables with no dependencies. Does this mean I could run a Go program on a server which does not have Go installed? If so, how do I package an application which I've written on my local machine to do this? I just want the easiest way possible just to test something very very basic.
Thanks!
评论:
Ajpennster:
int_hdlr:Just to let you know, Go does have a runtime. Its just apart of the compiled executable.
Franke123:Yes, it does have a runtime. What do you mean with "apart of the compiled executable"?
$ file ./hello ./hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, with debug_info, not stripped $ ldd ./hello not a dynamic executable
tiborvass:Yes Go binaries are portable. You can build a binary on an amd64 Linux dev computer and it will run on an amd64 Linux sever even if it doesn't have Go installed. To cross compile binaries (for a computer with a different OS, or architecture, or both) you can set the GOOS and GOARCH environment variables before building. See here: https://golang.org/doc/install/source#environment
Franke123:Just to clarify: Go binaries are not portable: you cannot run an amd64 linux binary on an arm64 windows :) Go code is mostly portable and cross compiling is as simple as it can get. The link you provided indeed has details about GOOS and GOARCH, but the whole document is about compiling Go itself from source which is not meant for people starting with Go. I would recommend instead the output of
go help environment
.
nesrednawerd:A binary can still be portable without being OS agnostic.
mrfrobozz:In the modern parlance of software development, "portable" means OS agnostic. Calling a binary portable because it can run on systems with the same architecture is a platitude.
yanc0:If they are coming from a DOS/Windows development environment, then portable can certainly mean "can be ran on a different system without runtime" since Windows exe's are in PE (Portable Executable) format.
likcoras:You don't have to install Go on the destination. All go binaries are statically compiled with all golang internals. You just have to choose the plateform type at the compilation time: GOARCH.
GerOnBears:...except if you use cgo or something.
In that case, you need to mess around with environment variables and linker flags a bit, iirc.
jackmott2:This is great feedback. Thank guys!
beowulf_71:you compile your program. it produces an exe you copy the exe to another machine, and it runs.
that is usually it, unless you include 3rd party libraries that require a dll/lib to run.
FierceDeity_:So thats another question.. I havent yet seen how you would call C or native code from Go, or, in the case of something like using GPU and needing a dll (windows example), how you bundle/include/distribute your application. I am guessing in this case, you start to look at an installation program to bundle the different bits of your app and to install them correctly? Or is it pretty simple to just zip up the exe and dlls (and anything else) and have it unzip into a dir.. no installation needed?
I am also guessing if you want auto integrated things like an icon on the Start/Task bar, that does require an installer of some sort?
jackmott2:With Windows, you can zip up the dlls, since Windows looks for libraries in the same folder first.
For Linux it's a little more complicated. But there are ways to package for Linux. Check out https://snapcraft.io/ for example or https://appimage.org/ for something that doesn't need the user to install anything.
It depends on the library but often yeah you just zip up the exe with the dlls it needs. This works with SDL2.
But yes if you want to put things in the start menu, set registry settings, or other OS integration you might want to use an installer.
