C & Go

A Brief Comparison, from Installation to Execution

Isshiki🐈
3 min readJul 18, 2020

Installation

C: you may or may not have a pre-installed C compiler in your machine but you can always install a new one. brew install gcc, for example, will install gcc in /usr/local/Cellar/gcc/{version}/bin alongside the standard library in /usr/local/Cellar/gcc/{version}/lib/ and its header file in /usr/local/Cellar/gcc/{version}/include/.

Go: you can download the Go compiler from its website. If you follow the official installation instructions, the compiler should live in /usr/local/go/bin alongside its precompiled packages in /usr/local/go/pkg and its standard library files in /usr/local/go/src. (Some tutorials, for some unknown reason, may tell you to modify your GOROOT, but GOROOT, /usr/local/go in this case, only indicates the location of your Go installation and therefore unless you have multiple versions of Go installed in your machine, it does’t make much sense to change it)

By the way, I think it makes learning C a lot easier to have header and library files right next to you…

File structure

C: files are quite independent of each other due to its separate compilation mechanism. Each C file can be compiled into either an executable (if it has a main function), a shared object file (.so) or an object file (.o) and multiple object files (.o) can then be archived into a larger static library file (.a).

Go: each file must belong to a package and must begin with a package name, like package pkg-name. Each package, containing one or more Go files, will be compiled into an executable or a library. The package that is gong to be compiled into an executable must be named main and have a main function.

Compilation

C: you compile C files by calling your preferred compiler and listing all of the files that need to be compiled, like gcc *.c.

Go: you can compile files by listing all of them like go build *.go too, but since Go files are organised as packages, you can specify a GOPATH in your shell config file to tell Go where your packages live and after that whenever you run go build pkg-name, Go will search your GOPATH for source files. By default your GOPATH is ~/go and your packages should go to ~/go/src/pkg-name-1. Of course you don’t have to use GOPATH but these options do exist to boost efficiency. (just heard that GOPATH will be deprecated soon?)

Libraries or Packages

C: to use the standard library, just include the header files you need and the compiler will search for them in the include directory as mentioned above and link the object files it finds under the lib directory to your own files. To use a third party library, you need to download library files from the hosting website, perhaps manually compile the source code files, move everything you need (presumably the include and bin directories) to your project directory and include its header file so that C’s compiler may know what’s in that library.

Go: to use the standard library, just add the names of the libraries you need to the import list, and Go will search GOROOT for these libraries. To use a third party package, you simply download and install that package, preferably using go get and import that library by adding its name to the import list.

Linking

C: for the linker to link a static library (and the math library) you will have to explicitly tell the compiler where it can find those files and which file you want to link with the link flag. For example, gcc -static -o prog main.c -L . -lvector tells the compiler to search for the static library libvector.a in the current directory

Go: to link third party libraries into your executable you simply add the names of those installed packages to your import list and run go build.

Executables

C: gcc will put the generated executable in the current directory unless you give it a location using the -o flag like gcc main.c -o bin/prog

Go: if you build a single single file, Go will generated an executable file in the current directory but if you build a package, the resulting executable will be placed in GOPATH/pkg/{platform}/ instead.

--

--