From C++ to Go: Demystifying Pointers, Slices, and Errors
Go code looks like C++, but the ideas behind it are different. Three things I had to learn again while building a todo app in Go: errors are values, a slice is a small header that points at the data, and the asterisk does two jobs.

When you come to Go from C++, the code looks very similar. Both languages have curly braces, structs, pointers and asterisks.
The ideas underneath are very different, though, and I think the biggest differences hide in the places where the two languages look the most alike. I just built a small todo app in Go, which keeps its tasks in memory. Three changes in thinking mattered most, and here they are in the order I ran into them.
1. Errors are values, not exceptions
Go doesn't have C++'s try and catch. There is no throw, and nothing can jump out of the middle of your function without you seeing it.
In Go, an error is an ordinary value that a function returns. You handle it like any other return value. You check it, act on it, or return it to your own caller.
To make your own kind of error, you don't need a special library. Go has a built-in interface called error, with one method, Error() string. Any type with that method is an error.
type TaskError struct {
message string
}
// Satisfying the built-in interface
func (e *TaskError) Error() string {
return e.message
}
That's all it takes. The caller receives the error as a second return value.
task, err := list.Find(id)
if err != nil {
// handle it here, visibly, or return it up the stack
}
There is a pointer detail here. The receiver is *TaskError, not TaskError, and the functions that create errors return a pointer to the struct. That's on purpose.
Suppose two parts of the app each create an error that says "task not found". If they were plain struct values, comparing them with == would say they are the same error, because Go compares structs field by field. If they are pointers, == compares their memory addresses instead. Every error you create then has its own address, so you can tell two errors apart even when their messages are the same.
The standard library does the same thing. errors.New returns a pointer, so two errors made by errors.New with the same text are never equal.
Coming from C++, having no exceptions felt like a step backwards at first. After a week I felt the opposite. Every path through a Go function is written in the function. Nothing interrupts it unless the function itself asks for that.
2. The three layers of a slice
My todo list was one line of code, type List []Task. Then I wrote a method with a pointer receiver, used *l to get the list, and had to stop and ask what I had just copied.
To answer that, you need to see that a slice is not the data itself. It's a small struct that describes where the data is. There are three separate layers.
- The pointer, of type
*List. It holds the memory address of the slice header. - The slice header. It's a small struct with exactly three fields: a pointer to the backing array, the length and the capacity. On a 64-bit machine that's 24 bytes.
- The backing array. This is the large block of memory where the tasks actually are.
So *l doesn't copy the backing array. It copies the 24-byte header, which is quick.
It works like a TV remote control. Copying the remote gives you a second remote, but both still control the same TV. In the same way, the original header and the copy both point at the same backing array. If you change a task through one of them, the caller sees the change. And passing a slice to a function is fast, because only the header is copied, never the data.
There is one thing the copied header can't do, and that's grow the caller's list. append might move the data to a new, bigger array, and it always returns a new header. So to grow the list, you have to write that new header back through the pointer.
func (l *List) Add(task Task) {
*l = append(*l, task) // write the new header where the caller can see it
}
If you have read Memory: The Stack, the Heap, and Pointers, you will recognize these layers. The header is small and cheap to copy. The backing array is the large part, on the heap. The design makes sure you only copy the small part.
3. The asterisk does two different jobs
In Go, the asterisk means two different things, depending on where it is.
In a type, as in l *List, it's part of the type's name. It reads left to right: "l is a pointer to a List."
In front of a variable, as in *l, it's an action. It means "go to the address stored in l and get the value there."
The same character appears twice in one short method of my todo app, doing two unrelated jobs. I wrote a full guide to this in The Two Jobs of the Asterisk. Below is the C++ history that explains why Go's version looks the way it does.
The great C and C++ pointer debate
C and C++ write declarations the other way round from Go, with the type first and the name second. The compiler ignores spaces, so the asterisk can sit next to either one. People have argued for decades about which is right, and there are two groups.
Group 1 is the modern C++ style, written List* l. The asterisk goes with the type. The reasoning is that "pointer to List" is the type, so the asterisk belongs with List.
Group 2 is the classic C style, written List *l. The asterisk goes with the variable. The reasoning is that a declaration should look like the way the variable is used: *l gives you a List, so write it that way.
The classic style exists mainly because of one well-known trap, which appears when you declare two variables on one line.
List* a, b;
That line looks like it declares two pointers. It doesn't. The compiler reads a as a pointer to a List, and b as a plain List. The asterisk belongs to the name that follows it, wherever you put the spaces. To get two pointers, you have to write the asterisk twice.
List *a, *b;
Because of this trap, many C programmers learned to put the asterisk next to the variable name, so that the code looks like what the compiler does. Modern C++ style guides prefer the asterisk next to the type, and add the rule "one declaration per line" so the trap can't happen.
How Go fixed it
Go avoids the debate completely. It puts the name first and the type second, so the question never comes up.
var a, b *List always declares two pointers. No placement of spaces can change that. The declaration reads from left to right, the same way you would say it aloud.
Rob Pike, one of Go's designers, has written that Go's type syntax was designed this way because C's style became very hard to read once types got complicated.

It's a small detail, and it shows how Go is designed in general. C++ keeps the flexibility and leaves the danger to style guides. Go removes the choice, so the danger can't exist. The debate has enough grammar and history behind it for its own post, List* l or List *l?.
The shift in one sentence
C++ gives you full control of the machine and trusts you with it. Go gives you a smaller set of tools, designed so that three classic mistakes are hard to make: hidden jumps out of a function, copying large data by accident, and confusing declarations.
Neither approach is wrong. But C++ habits that you bring into Go without thinking tend to work against Go in exactly the places where it's trying to help.
If you want the two languages side by side in more depth, start with Go 101 and C++ 101.
Check yourself
3 questions · nothing is saved- Written
- Apr 14, 2026
- Length
- 1,349 words · 6 min
- Built
- …
- Served by
- …
- Delivered
- …
- Over the wire
- …
- Go's share
- …
Why Go Rewrites 0644 as 0o644 (and Why chmod Won't Take 'rwx')
I typed 0644, saved, and my editor changed it to 0o644. That lowercase o fixes a kind of bug that is fifty years old, and it also explains why file permissions are numbers and not letters.
- 1C++ 101: The Language That Trusts You Completely
- 2Go 101: A Small Language for Big Systems
- 3The Two Jobs of the Asterisk: Pointers in Go and C++
- 4List* l or List *l? The Oldest Style War in Programming
- 5From C++ to Go: Demystifying Pointers, Slices, and Errors
- 6Why Go Rewrites 0644 as 0o644 (and Why chmod Won't Take 'rwx')
Memory: The Stack, the Heap, and Pointers
Where your variables actually live, why the heap costs more than the stack, and what a pointer really is.
OpenCV 5: What Actually Changed Under the Hood
The first major version since 2018 is about foundations, not new features: a rebuilt engine that runs over 80% of ONNX instead of 22%, real n-dimensional arrays, fast code from four chip makers, and a list of removals long enough to break your build.
OpenCV 101: Every Image Is a Grid of Numbers
A photo is a rectangle of numbers, and everything OpenCV does is arithmetic on that rectangle. Mat, BGR, convolution, thresholds, contours and perspective warps: the ideas that make the library's 2,500 functions easier to find your way around.
Projects built with what this post is about.


