Tooling

A language is also the set of tools that come with it. Go ships a formatter, a documentation reader, a static checker, a debugger interface, a profiler, a coverage tool and a disassembler, and every Go installation has all of them. C++ ships a compiler and a linker; everything else is a choice a project makes, and each choice is a version to install. This page runs the tools of both, and says which ones this container does not have.

The formatter

gofmt reads a file, prints it in one canonical layout and writes nothing unless it is asked. The file below is deliberately badly spaced; the diff shows exactly what the tool would change.

package tooling import "fmt" func shout(name string) string { return fmt.Sprint("HELLO, ", name) }

gofmt -d .

diff tooling/messy.go.orig tooling/messy.go
--- tooling/messy.go.orig
+++ tooling/messy.go
@@ -2,6 +2,6 @@

import "fmt"

-func shout(name string)string{
- return fmt.Sprint("HELLO, ",name)
+func shout(name string) string {
+ return fmt.Sprint("HELLO, ", name)
}

gofmt -w tooling/messy.go
gofmt -l .

The second command printed no file name, so the tree is formatted. Go has no configuration for this: no line length, no brace style, no tabs-versus-spaces option. A team that dislikes a rule cannot change it, and that is the point, because a diff then contains only changes of meaning. C++ has clang-format and astyle, both good, both configured per project with a file and a style, and a project that has not configured one has no formatter at all. clang-format is not installed in this image, so the page shows no run of it.

Documentation

A doc comment is a comment that starts with the name of the thing it documents, placed directly above it. go doc prints the comments of a package without generating a site:

// Package tooling is a small example for the tooling page. package tooling import "fmt" // Greeting returns a greeting for name. func Greeting(name string) string { return fmt.Sprintf("hello, %s", name) }

go doc ./tooling

package tooling // import "example.com/tooling/tooling"

Package tooling is a small example for the tooling page.

func Greeting(name string) string

go doc fmt.Println prints the documentation of the standard library the same way, which is how a Go programmer reads a package without a browser. C++ has Doxygen, which is more powerful and has to be installed, configured and run to produce a site; this image does not have it either. A C++ header read cold is a header with comments, which is also fine and needs no tool.

Warnings and vet

A C++ compiler warns about a mistake only when it is asked. Two warnings in one function, with -Wall -Wextra:

#include <cstddef> int main() { int unused = 42; std::size_t size = 3; int index = -1; if (size < index) { return 1; } return 0; }

g++ -std=c++23 -O2 -Wall -Wextra -c warn.cpp -o warn.o

warn.cpp: In function 'int main()':
warn.cpp:8:14: warning: comparison of integer expressions of different signedness: 'std::size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
8 | if (size < index) {
| ~~~~~^~~~~~~
warn.cpp:5:9: warning: unused variable 'unused' [-Wunused-variable]
5 | int unused = 42;
| ^~~~~~

The default of g++ is much quieter: without -Wall the two warnings disappear. Go has no warning flags because its compiler rejects most of what the C++ one warns about, and go vet covers the rest, on by default in go test for the checks that are almost never wrong. The go vet page of the testing group shows what it finds.

Undefined behaviour

Signed integer overflow is undefined behaviour in C++, and GCC has a sanitizer that reports it at run time. The program adds one to the largest int:

#include <iostream> int main() { int value = 2147483647; value += 1; std::cout << value << "\n"; }

g++ -std=c++23 -O1 -g -fsanitize=undefined ub.cpp -o ub
./ub; echo "status: $?"

ub.cpp:6:11: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'
-2147483648
status: 0

The report names the file, the line and the operation, and the program keeps running because the sanitizer recovers by default. In Go the same overflow is defined: it wraps, and the program must be built with a tool that checks it:

package main import "fmt" func main() { value := int32(2147483647) value++ fmt.Println(value) }

go run ./wrap

-2147483648

Go has no undefined behaviour, so it has nothing to sanitize. The price is that a wraparound is a silent bug rather than a reported one, and the tool that finds it is a test with a boundary case, or go vet where the mistake is visible in the source. The C++ sanitizer is the better tool for this bug; the Go language is the better default.

The debugger and the disassembler

gdb is installed here and it works on a Go binary, because a Go binary is an ordinary ELF file with DWARF information. The binary below is the hello world from the size page, built with -trimpath so the paths in the output are relative:

gdb -batch -ex 'set auto-load safe-path /' -ex 'set disable-randomization off' \
-ex 'break main.main' -ex run -ex 'bt 2' ./hello_go

Thread 1 "hello_go" hit Breakpoint 1, main.main () at ./hello.go:5
5 func main() {
#0 main.main () at ./hello.go:5

Two warnings that gdb prints above these lines are the container: it may not lower the ASLR entropy and it refuses to auto-load the Go runtime helpers. The breakpoint, the source line and the backtrace are the same for a C++ binary with break main. What is missing is a Go-aware debugger: Delve understands goroutines, channels and the scheduler, and it is not installed here.

The toolchain also disassembles without an external program, with the Go source lines in the left column:

go tool objdump -s 'main.main' hello_go | head -6

TEXT main.main(SB) ./hello.go
hello.go:5 0x49e120 493b6610 CMPQ SP, 0x10(R14)
hello.go:5 0x49e124 7646 JBE 0x49e16c
hello.go:5 0x49e126 55 PUSHQ BP
hello.go:5 0x49e127 4889e5 MOVQ SP, BP
hello.go:5 0x49e12a 4883ec38 SUBQ $0x38, SP

What is in the box

Task C++ Go In this image
Format clang-format, astyle, uncrustify gofmt Go only
Lint clang-tidy, cppcheck, compiler warnings go vet, staticcheck g++ and go vet only
Docs Doxygen, Sphinx go doc Go only
Debug gdb, lldb gdb, Delve gdb only
Profile perf, Valgrind, gprof pprof, runtime traces Valgrind and pprof only
Dynamic check ASan, UBSan, TSan race detector, bounds checks UBSan and the race detector

The Go column is shorter and identical in every installation, which is worth a lot on a team. The C++ column has better tools: clang-tidy reasons about the type system, perf profiles without recompiling, Valgrind finds leaks a collector makes impossible. A C++ team that invests in that tooling has a better equipped shop than a Go team; a C++ team that does not has none of it, and the default is the compiler and printf.

The Go lessons behind these tools are an editor, gofmt, go vet and go doc and go vet, staticcheck and golangci-lint. The previous page of this group is refactoring; the next one is onboarding.

The C++ programs were built with g++ 15.2.0 (Alpine 15.2.0) and the Go programs with go 1.26.10; gdb 16.3 debugged the Go binary, all on Alpine Linux 3.24.3 (x86_64) in a Docker container. clang-format, clang-tidy, Doxygen, Delve and perf are absent and nothing on this page claims to have run them.

Article author: Arthur Isaev

Banner image