39

I have been researching Golang and I see that it has a compiler. But is it compiling Go into assembly level code or just converting it into BYTECODES and then calling that compilation? I mean, even in PHP we are able to convert it into BYTECODES and have faster performance. Is Golang a REPLACEMENT for system level programming and compiling ?

Olsonist
  • 1,208
  • 1
  • 10
  • 30

2 Answers2

63

This is really a compiler (in fact it embbeds 2 compilers) and it makes totally self sufficient executables. You don't need any supplementary library or any kind of runtime to execute it on your server. You just have to have it compiled for your target computer architecture.

From the documentation :

There are two official Go compiler tool chains. This document focuses on the gc Go compiler and tools (6g, 8g etc.). For information on how to work on gccgo, a more traditional compiler using the GCC back end, see Setting up and using gccgo.

The Go compilers support three instruction sets. There are important differences in the quality of the compilers for the different architectures.

amd64 (a.k.a. x86-64); 6g,6l,6c,6a A mature implementation. The compiler has an effective optimizer (registerizer) and generates good code (although gccgo can do noticeably better sometimes).

386 (a.k.a. x86 or x86-32); 8g,8l,8c,8a Comparable to the amd64 port.

arm (a.k.a. ARM); 5g,5l,5c,5a Supports only Linux binaries. Less widely used than the other ports and therefore not as thoroughly tested.

Except for things like low-level operating system interface code, the run-time support is the same in all ports and includes a mark-and-sweep garbage collector, efficient array and string slicing, and support for efficient goroutines, such as stacks that grow and shrink on demand.

The compilers can target the FreeBSD, Linux, NetBSD, OpenBSD, OS X (Darwin), and Windows operating systems. The full set of supported combinations is listed in the discussion of environment variables below.

On a server you'll usually target the amd64 platform.

Note that Go is well known for the speed of compilation. When deploying my server programs, I don't build for the different platforms on the development computer : I deploy the sources and I compile directly on the production servers. Since Go1 I never had a code compiling on one platform and not compiling on the other ones.

On Windows I had no problem in making an exe on my development computer and simply sending this exe to people never having installed anything Go related.

Community
  • 1
  • 1
Denys Séguret
  • 335,116
  • 73
  • 720
  • 697
  • So its not converting it into bytecodes and actually compiling. –  Sep 03 '12 at 13:50
  • Yes. it's compiling exactly in the same sense any C program is compiled. Everything is statically linked. – Denys Séguret Sep 03 '12 at 13:51
  • 13
    @Skaperen @nos You can pass -S to the compiler to see the assembly code. `go build -gcflags -S` will run the compiler, passing -S to output assembly. You can also invoke the compiler directly, e.g. `go tool 8g -S ` – axw Sep 04 '12 at 01:37
  • Why does golang have runtime which does garbage collection etc. how does the runtime manage the compiled exe? – ProgramCpp Sep 08 '17 at 15:43
  • 1
    @ProgramCpp the 'runtime' is embedded in the exe. – wingerse Mar 13 '18 at 17:40
0

Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It's a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.

Source - golang.org

Matze
  • 4,433
  • 4
  • 45
  • 59