<p>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.</p>
<p>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.</p>
<p>Thanks! </p>
<hr/>**评论:**<br/><br/>Ajpennster: <pre><p>Just to let you know, Go does have a runtime. Its just apart of the compiled executable. </p></pre>int_hdlr: <pre><p>Yes, it does have a runtime. What do you mean with "apart of the compiled executable"?</p>
<pre><code>$ 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
</code></pre></pre>Franke123: <pre><p>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: <a href="https://golang.org/doc/install/source#environment">https://golang.org/doc/install/source#environment</a></p></pre>tiborvass: <pre><p>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 <code>go help environment</code>.</p></pre>Franke123: <pre><p>A binary can still be portable without being OS agnostic.</p></pre>nesrednawerd: <pre><p>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.</p></pre>mrfrobozz: <pre><p>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.</p></pre>yanc0: <pre><p>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.</p></pre>likcoras: <pre><p>...except if you use cgo or something.</p>
<p>In that case, you need to mess around with environment variables and linker flags a bit, iirc.</p></pre>GerOnBears: <pre><p>This is great feedback. Thank guys!</p></pre>jackmott2: <pre><p>you compile your program.
it produces an exe
you copy the exe to another machine, and it runs.</p>
<p>that is usually it, unless you include 3rd party libraries that require a dll/lib to run.</p></pre>beowulf_71: <pre><p>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?</p>
<p>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?</p></pre>FierceDeity_: <pre><p>With Windows, you can zip up the dlls, since Windows looks for libraries in the same folder first. </p>
<p>For Linux it's a little more complicated. But there are ways to package for Linux. Check out <a href="https://snapcraft.io/" rel="nofollow">https://snapcraft.io/</a> for example or <a href="https://appimage.org/" rel="nofollow">https://appimage.org/</a> for something that doesn't need the user to install anything. </p></pre>jackmott2: <pre><p>It depends on the library but often yeah you just zip up the exe with the dlls it needs. This works with SDL2.</p>
<p>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.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传