From C to Go
My journey jumping 50 years into the future by transitioning from C to Go.
How it started – Being in the embedded programming industry for more than 30 years, C was until recently practically the only real programming environment to build projects for a large variety of platforms. Go finally provides a modern alternative.
Hello World in Go vs C – Although the Hello World programs for C and Go look surprisingly similar, there are important differences in the concepts of both languages. By showing these differences in a small example program like Hello World, it is easier to understand the differences how development of larger projects in Go should be approached.
Variables in Go – The declaration of variables in Go is slightly different from the way it is done in C. The main differences are explained here. The example program also shows an inherent design flaw in Go regarding detecting shadowing global variables by local variables sharing the same name.
Parallel processing in Go – Parallel processing is not just possible in Go, it is the core functionality. The programming statement go is defined for doing just that, starting new goroutines. An example program is discussed here where two processes run in parallel and the main routine only exits after both routines have completed. This example program can used as a simple template for build your own parallel programs.
Synchronizing Goroutines – In many cases it is necessary to synchronize running goroutines with each other. This example shows a simple way to do that with one variable which contains the state of the outer main program loop. For cases where more sophisticated synchronization is needed, Go
channels can be used, which are discussed in other examples.
Calling C from Go – Go is designed to be the twentyfirst century replacement for C. Its target audience is the experienced C programmer who needs a robust programming language for system programming. This page explains with an example how existing C code can be imported and reused in a Go program.
Shared Memory Access – Shared memory can be used as a way store resources used by multiple resources, but data corruption may take place when multiple goroutines try to change the same information at the same time. This example shows the wrong and correct way to used shared resources. The example also shows how easy it is to spawn a million goroutines in a matter of seconds on relatively modest hardware.
Time (to come) and Go – Time is an important concept in a programming language. It can be represented in either an absolute way as a reference to the time in the outside world, or in a relative way for internal time measurement and calculations in a Go program itself. The way time has been implemented in Go, including some common problems, is discussed with examples.
Go as a Web Server – One of the main reasons for me to start the C to Go transition is the need for a flexible scalable web server which can be embedded in existing or new applications. The example discussed in this page discusses the first simple web server in Go which in a few lines of code can both send and receive data to and from clients.
Boredom is a vital problem for the moralist, since at least half of the sins of mankind are caused by the fear of it.