Comparing Go vs. C in embedded applications

Impossibly tight deadlines, unrealistic schedules, and fixed strain to develop and launch functions on time, whereas on the similar time reaching wonderful high quality. Sound acquainted? Sooner time-to-market for embedded functions is certainly a crucial consideration for embedded growth groups. However how do you develop an method for delivering functions sooner whereas sustaining high quality and safety as a precedence? On this context, the product engineering staff behind the OTA software program replace supervisor, went by way of the method of choosing the right programming language for creating each the embedded shopper and server components of Mender. On this analysis course of Go, C, and C++ have been shortlisted as candidates. A few of the classes discovered from this analysis course of are defined and why Go was in the end chosen to develop Mender’s shopper embedded utility. 

Whereas this may be fairly subjective, Go is usually a very productive language for embedded growth. That is very true in the case of community programming, which is sooner or later part of each linked machine or utility. Go was created by Google to fulfill the wants of Google builders, and it was primarily developed to attempt to repair the explosion of complexity inside its ecosystem. So environment friendly compilation, environment friendly execution, and ease of programming have been the three primary rules behind creating Go as not all three have been accessible earlier than in the identical mainstream language. 

Nonetheless, we need to stress that Go can’t be thought-about a substitute for C as there are various locations the place C is and sure can be wanted, comparable to within the growth of actual time working methods or machine drivers. 

Strict necessities for embedded growth

After establishing the structure, the Mender product engineering staff evaluated which language could be the most effective match to develop the Mender utility. The system was presupposed to include a shopper working on the embedded machine and server performing as a central level the place purchasers have been linked to. As such, we had a number of necessities for the language: 

  • Because the shopper utility could be working on embedded units, compiled binaries wanted to be as small as attainable. 
  • It wanted to work with Yocto, the embedded distribution of Linux.
  • The complexity of set up purchasers ought to be low, thus limiting exterior dependencies and libraries.
  • As it might run on completely different units, the language should compile throughout completely different architectures. 
  • The units that the Mender shopper utility would run on could be an IoT or networked machine, so the language wanted entry to networking libraries. 

The necessities for choosing a brand new language additionally lined just a few non-functional necessities: 

  • As many programmers as attainable inside our group wanted to have the ability to perceive the language. 
  • It needed to be as straightforward as attainable to share and reuse the prevailing code between present functions written in C and reuse  code between the shopper and server functions. 
  • Growth velocity was additionally thought-about—we now have ongoing strain so as to add new options shortly. 

A comparability between Go, C, and C++ is printed in Determine 1. From the comparability, Go was primarily chosen for its help of buffer overflow safety, automated reminiscence administration, use of ordinary information containers, and its built-in help for JSON, HTTP, and SSL/TLS libraries. 

Requirement C C++ Go
Measurement necessities in units Lowest Low (1.8MB extra) Low (2.1 MB extra, nevertheless will enhance with extra binaries)
Setup necessities in Yocto None None None
Buffer beneath/overflow safety None Little Sure
Code reuse/sharing from CFEngine Good Simple (full backwards compatibility) Can import C API
Computerized reminiscence administration No Out there, however not enforced Sure
Customary information containers No Sure Sure
JSON json-c jsoncpp Constructed-in
HTTP library curl curl Constructed-in
SSL/TLS OpenSSL OpenSSL Constructed-in (TLS is part of crypto/tls package deal)*
Determine 1: A desk comparability of options throughout Go, C, and C++ 

The analysis continued to see how Go would stack up in opposition to C, C++, and C++/Qt for making a full-blown customized Linux picture utilizing Yocto. Pure picture dimension and picture dimension with community stack have been in contrast, as was how the top utility could be delivered to a goal picture.  As Go functions could be statically compiled to a single binary, there isn’t any want for any further digital setting (like within the case of Java, which requires a digital machine along with binaries) or further dependencies working on the machine to make use of the Go code. 

C C++ C++/Qt Go
Measurement overhead of the check element, and all the brand new libraries pulled in, when utilizing the smallest accessible Yocto picture as a foundation 8.4MB 10.2MB 20.8MB 14.6MB
Measurement with community stack 13.4MB (curl) 15.2MB (curl) 20.8MB* 14.6MB
Shared dependencies Sure Sure Sure No
Further Yocto layer wanted No No Sure No
Deployment complexity Binary Binary Binary + Qt Binary
Determine 2: How Go compares to different programming languages for Yocto builds

Evaluating Go vs. C

Go was additionally chosen for growth attributable to its extraordinarily wealthy normal library, which permits for a lot sooner growth, particularly when in comparison with C. Go inherits plenty of parts from C, C++, and Python together with the expression syntax, management circulation statements, information buildings, interfaces, pointers, the cross by worth idea, parameter parsing syntax, string dealing with, and rubbish assortment. With its optimized compiler, Go runs natively on embedded units. 

Go did have some disadvantages, which we’ll point out beneath. They didn’t weigh closely sufficient to beat its benefits, like the event velocity and ease of constructing within the language, however they did issue into our resolution. Right here’s the opposite qualities that went into our resolution. 

Measurement comparability of Go vs. C

Go is bulkier than C in the case of dimension, which was one of many few disadvantages it had. In the event you take “whats up world” because the smallest utility you may write in Go and use the built-in println operate, after stripping away debugging symbols it’s simply over 600 kB in dimension (snippet 1). In the event you embody the fmt package deal with its dependencies, then the dimensions will increase to 1.5 MB (snippet 2)

To match this to C, if you’re constructing a dynamically linked library, then it’s a mere 8 kbytes. In the event you do it static, then the dimensions will increase to over 800 kbytes which is surprisingly even bigger than the Go binary (snippet 3)

Snippet 1: smallest Go “whats up world” code:

package deal primary
func primary() 
   println("whats up world")

$ go construct
     > 938K
$ go construct -ldflags ‘-s -w’
     > 682K
$ go construct & strip
     > 623K

Snippet 2:  Go “normal” “whats up world”

#embody <stdio.h>
int primary(void)

  printf("whats up worldn");
  return 0;
$ go construct
    > 1,5M

Snippet 3:  C “whats up world”

#embody <stdio.h>
int primary(void)
#embody <stdio.h>
int primary(void)

  printf("whats up worldn");
  return 0;
$ gcc primary.c 
> 8,5K
$ ldd a.out 
> /lib64/
$ gcc -static primary.c 
> 892K
$ gcc -static primary.c & strip
> 821K

Pace comparability of Go vs. C

Compiled Go code is usually slower than C executables. Go is totally rubbish collected and this itself slows issues down. With C, you may determine exactly the place you need to allocate reminiscence for the variables and whether or not that’s on the stack or on the heap. With Go, the compiler tries to make an informed resolution on the place to allocate the variables. You possibly can see the place the variables can be allotted (go construct -gcflags -m), however you can not pressure the compiler to make use of solely the stack, for instance. 

Nonetheless, in the case of velocity we cannot overlook about compilation velocity and developer velocity. Go gives extraordinarily quick compilation; for instance, 15,000 traces of Go shopper code takes 1.4 seconds to compile. Go could be very nicely designed for  concurrent execution (goroutines and channels) and the aforementioned wealthy normal library covers many of the fundamental wants, so growth is quicker.

Compilation and cross compilation

There are two Go compilers you need to use: the unique one is named gc. It’s a part of the default set up and is written and maintained by Google. The second is named gccgo and is a frontend for GCC. With gccgo, compilation is extraordinarily quick and enormous modules could be compiled inside seconds. As talked about already, Go by default compiles statically so {that a} single binary is created with no further dependencies or want for digital machines. It’s attainable to create and use shared libraries utilizing the -linkshared flag. Ordinarily, Go requires no construct recordsdata and a construct could be carried out by a easy “go construct” command, however it’s also attainable to make use of a Makefile for extra superior construct procedures. Right here’s the Makefile we use at Mender. 

On high of the above, Go helps a lot of OSes and architectures in the case of cross compilation. Determine 3 highlights the big range of OSs and platforms supported by Go. 

GO OS / GO ARCH amd64 386 arm arm64 ppc64le ppc64 mips64le mips64 mipsle mips wasm risc64
aix X
android X X X X
darwin X X
dragonfly X
freebsd X X X
illumos X
ios X
js X
linux X X X X X X X X X X X
netbsd X X X
openbsd X X X X
plan9 X X X
solaris X
home windows X X X X

Debugging and testing in Go

Many builders, with a purpose to see what’s occurring inside a program whereas it executes, will use GDB.  For closely concurrent Go functions, GDB has some points in attempting to debug them. Thankfully, there’s a devoted Go debugger referred to as Delve that’s extra appropriate for this goal. Builders solely acquainted with GDB ought to  be capable of use it in majority of instances with none points

This can be very straightforward so as to add testing and unit testing to Go code. Testing is constructed into the language—all that’s wanted is to create a file with a “check” suffix, add a check prefix to your operate, import the testing package deal, after which run “go check”. All of your assessments can be mechanically picked up out of your supply and executed accordingly. 

Concurrency help

Concurrency is nicely supported in Go. It has two built-in mechanisms: goroutines and channels. goroutines are light-weight threads simply 2 kB in dimension.  It is vitally straightforward to create a goroutine: you solely want so as to add the “go” key phrase in entrance of the operate and it’ll execute concurrently. Go has its personal inside scheduler, and goroutines could be multiplexed into OS threads as required . Channels are “pipes” for exchanging messages between goroutines, which could be both blocking and non-blocking. 

Static and dynamic libraries; C code in Go

Go has many much less recognized options that can help you cross directions to the compiler, linker, and different components of the toolchain utilizing particular flags. These embody the  -buildmode and -linkshared flags, which can be utilized to create and use static and dynamic libraries each for Go and C snippets. With a mixture of particular flags, you may create static or dynamic libraries with generated header recordsdata for C, which could be invoked by C code afterward. 

That’s proper! With Go we will name C code and have the most effective of each worlds. The cgo instrument, which is a part of the Go distribution, can execute C code, which makes it attainable to reuse our personal or system C libraries. In reality, cgo and implicit utilization of C system libraries is extra fashionable than one would possibly assume. There are instances when normal C library routines are picked over pure Go implementations by the Go language constructs themselves.

For instance, when utilizing a community package deal on Unix methods, there’s a massive likelihood {that a} cgo-based resolver can be picked. It occurs to name C library routines (getaddrinfo and getnameinfo) for resolving names in instances when native Go resolver cannot be used (for instance on OS X when direct DNS calls should not allowed). One other frequent case could be when the os/person package deal is used. If you wish to skip constructing cgo components you may cross osusergo and netgo tags (go construct -tags osusergo,netgo) or disable cgo utterly with CGO_ENABLED=0 variable. 

There are difficult issues to look out for when mixing C and Go collectively. As a result of Go is rubbish collected however C is just not, so if you’re utilizing any C variables inside Go code which can be allotted on the heap within the C code, then it’s worthwhile to explicitly free these variables inside Go.

C++ may also be used inside Go code, which is facilitated by way of a simplified wrapper and interface generator (SWIG) that binds C and C++ with languages comparable to Python. 

Go benefits outweigh the disadvantages

Our expertise utilizing Go reveals each benefits and drawbacks. Beginning with the negatives, there are plenty of exterior libraries locally however the high quality varies tremendously and it’s worthwhile to be very cautious with what sorts of libraries you employ. That is bettering tremendously because the language matures and beneficial properties adoption. 

When you’ve got some C bindings inside your Go code, issues often get extra difficult, particularly cross compiling, which can’t be finished simply with out importing the entire C cross-compilation infrastructure. 

There are nonetheless plenty of positives about utilizing Go for embedded growth. It’s fast and simple to transition from C and/or Python to Go and get productive inside a few days. Go presents very good tooling and a normal library with greater than 100 packages. You possibly can simply set and management runtime settings comparable to what number of OS threads you need to use (GOMAXPROCS), or if you wish to swap on or off rubbish assortment (GOGC=off). Libraries and code could be shared simply between the server and shopper growth groups like in our case. 

It might be controversial to say this, however the pressured coding normal quickens growth. Whatever the code you’re inspecting, it at all times appears the identical. There isn’t any time wasted questioning which coding normal to make use of. 

We developed just a few demos of embedded Go functions which you could play with utilizing fashionable {hardware} comparable to Beaglebone Black or Raspberry Pi: One is named “Thermostat” and is predicated on Beaglebone {hardware}. It makes use of infrared distance, temperature, and humidity sensors and  exports all of the sensor readings by way of an internet interface. The opposite one is a PRi-based robotic automobile with a digital camera that may be managed utilizing a offered internet interface. Get pleasure from!

Marcin Pasinski is the Chief Embedded Engineer at If you wish to be taught extra about the most effective approaches to over-the-air software program updates for IoT gateway units, go to and check out the OTA software program updater for free

Tags: c, embedded programming, go, golang, IoT

More Posts