Go vs C++ Summary
Go is a simple, compact, and procedural general purpose language. C++ is a general purpose coding language that is fast and complex. Both Go and C++ are statically typed and have strong user communities. C++ is used in a wide range of applications, while Go is most used for web backends.
C++ is widely used. As a systems programming language, it serves as the backbone for a huge number of programs, computing tasks, and other programming languages. It sits on a plethora of platforms and is used for developing everything from video games to the programs that drive space probes. It has been in use for a long time, and it’s been put through its paces.
Go programming (or Golang) is almost brand new on the programming scene. Made by Google, it’s targeted at replacing C++ as the go-to general-purpose systems programming language, and it’s built specifically for taking that spot. So which is better: The underdog or the aging champion?
Go (Golang) Programming
As programming languages go (no pun intended), Go is pretty new. It was created by Rob Pike, Robert Griesemer and Ken Thompson specifically for Google. It is a statically-typed, compiled, and general-purpose programming language much like C++. The compiler for the language was originally written in C but is now written in Go as well, which keeps the language self-hosted.
Go, as well as many of its IDEs and libraries, is also distributed under the appealing open-source license.
Go is made for modern multicore processors. The language supports, and exceeds at, concurrent programming; meaning it can run multiple processes at the same time using different threads rather than just running one task at a time. It also has deferred garbage collection that performs memory management to prevent memory leaks.
C++ is one of the most widely-used programming languages in the world. It’s a compiled, middle-level, object-oriented programming language built with performance and efficiency in mind. C++ is built for everything. Fast and limitation-friendly C++ (and it’s C cousins) form the backbone of a large part of the computer world.
C++ was created some time ago, in 1979, when a Danish computer scientist named Bjarne Stroustrup wanted to make an extension to C that would allow it to use classes. Now C++ is used everywhere. It’s even used to write compilers and interpreters for other languages.
Comparing Go vs C++
Now that we know a little about the origins of each language, let’s put them in the ring together and see how they stand toe to toe in the following categories:
Go vs C++: Speed and Readability
C++ has been called a DIY language, so while it may not come with a lot of features, you can build any feature you want if you know the language well enough.
On the same note, C++ is considered a mid-level language, so it’s not as lingual and intuitive as high-level languages, but it’s not as rough as, say, an assembly language.
However, that does mean it’s more complex to code in than a higher-level language. Something that might take a couple of lines in a language like Python could take a dozen in C++.
Go code is more compact. It’s built around simplicity and scalability. It pares away unnecessary brackets and parentheses, but it still leaves less leeway for error.
It’s also statically typed like C++, meaning the programmer must declare every variable type. However, Go is much easier to learn and code in than C++ because it is simpler and more compact. It also has some built-in features that don’t need to be written for every project (like garbage collection), and those features work well.
Another consideration is compile-time. C++ has a notoriously slow compile-time. While compile-time is dependant on what you’re actually coding, Go is significantly faster to compile over C++.
Since your code needs to be compiled before it’s run and compiled again after every change you make, compile-time matters for coding speed. When you need to run the code over and over again to find the one missing semicolon in your C++ code, those compile times stack up quickly.
It’s also worth mentioning data structure. C++ sports the well known and familiar object-oriented structure, whereas Go is a procedural and concurrent programming language. Unlike C++, Go doesn’t have classes with constructors and deconstructors.
C++ vs Go: Performance
Go is very fast compared to other high-level programming languages. It’s compilation, static types, and efficient garbage collector make it extremely quick. Go is also good at memory management; it has pointers instead of references. Golang can boast speeds of close to four times quicker than it’s interpreted and dynamic friends.
That said, very little can touch C++ (and most C languages) when it comes to speed. All of the time spent coding and compiling pays off here. Because C++ is a hard to code, middle-level, language, it’s closer to the machine code: and when compiled it fits into that machine code neater.
It also lacks those features that make coding easier but add drag to the resulting program. When it comes time to run, C++ is light, lean, and fast.
Go comes equipped with all those parts and pieces that made your life easier during the coding process, so it does run slower. One of the biggest chunks is its slow, albeit excellent, garbage collector.
While garbage collecting is normally a death knell— signaling a slow-performing language—Go’s is highly optimized. However, it’s still a garbage collector and it will still slow down the code compared to not having one at all.
To be fair, Go isn’t magnitudes slower than C++. Unless your program must maximize speed above all else, Go will work just as well as C++. It’s unlikely the speed difference will be big enough to notice unless you’re doing massive calculations.
Go vs C++: Security
Some of the worst security vulnerabilities in C language programs involve taking advantage of buffer overflows. This is when a buffer is loaded with too much information and that information ends up getting written in adjacent memory. This can create a crash, or as many have discovered, a hole with which to gain access to a once airtight program.
Go has limitations built in to help prevent this problem. For example, Go does not allow pointer arithmetic. You cannot step through arrays using pointer values (you must access these elements via index). Doing things this way forces the programmer to use a method that includes bounds checking, which prevents an overflow.
It should be noted, however, that buffer overflows aren’t an inherent vulnerability in all C++ programs. The methodology that’s forced in Go is also possible in C++, the only difference is that C++ allows a programmer to be lazy and create these vulnerabilities.
C++ vs Go: Application
Limitless application is one of the main reasons C++ still holds its own against Go. C++ is an open book. A programmer and subsequently a program can access every part of the source code itself and the machine running it.
It doesn’t have any built-in features to turn on or off either, it’s a clean slate for creating programs and systems. That’s why it’s even possible to create an operating system in C++; you have access to everything.
Go, on the other hand, is more of a sealed system. It’s much more difficult to access Go’s inner workings. For example, take Go’s infamous garbage collection that works so well. If a programmer wanted to modify how that garbage collection works, or whether it’s there at all, they will have an incredibly hard time doing so.
While Go is an excellent language, it’s not designed to function as ‘low’ as C++ can. Because of this, Go isn’t as widely used as C++, and currently the most common place to see Go is the web backend.
Go vs C++: Community
C++ has been around a while. It has a large community behind it and therefore there are answers to nearly any question you might have for C++. If you need an integration, someone’s probably made it, or more likely, whatever you’re integrating to already has features to integrate with the code you’re writing.
However, there is a price tag attached. C++ is old; so many of its libraries, modules, and tutorials are out of date. It’s up to you to find a solution that isn’t just applicable but modern as well.
Go is newer, with fewer use cases and fewer people behind the language. Until recently, the documentation was so scarce many programmers were not interested in the language at all.
However, while its library is smaller than C++’s, Go is a newer language. It doesn’t have all of the dusty old dev kits, advice, and integrations from 1998 still lurking on the web. Everything you might find for Go will be new and nearly cutting edge. All of the existing code for Go works and is written to fit modern standards for development.
Go’s community is also more alive. Since it’s a new language the community around it is still excited to discover what it can do, and all of the existing content that C++ already has is what’s being made now by Go programmers and developers. It’s exciting to be a part of a new language where there are still corners to be explored and features to be developed.
If developing for either language sounds fun to you, don’t hesitate to choose one over the other. While Go might not be replacing C++ any time soon, it’s still used often and is in high demand. While they may have their different strong suits as languages, they stack up well against each other and you can’t really go wrong with either.