L5 · LanguagesProgramming Languages 101 · part 5 of 6

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.

Takumi Yamada
· 6 min read
gocpppointerssliceserrorsprogramming-languages
The C++ logo, a blue hexagon with the letter C, next to the Go logo, the word GO in light blue.
The two languages in this post. C++ first came out in 1985, and Go in 2009.Left, logo: Jeremy Kratz for the Standard C++ Foundation, public domain (trademark), via Wikimedia Commons (original). Changes: placed on a background.Right, logo: The Go Authors, public domain (trademark), via Wikimedia Commons (original). Changes: placed on a background.

When you come to Go from C++, the code looks very similar. Both languages have curly braces, , 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.

C++an exceptionf()g()h()f calls g, and g calls h, where it failsthrowcatchnothing in g()shows it passingGOa returned valuef()g()h()f calls g, and g calls h, where it failsreturn errreturn errif err != nil
Where control goes after a failure. The Go path is written out in every function it passes through.

To make your own kind of error, you don't need a special library. Go has a built-in 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 instead. Every error you create then has its own address, so you can tell two errors apart even when their messages are the same.

VALUEScompared by fieldtask not founderror 1==task not founderror 2true: the twocan't be told apartPOINTERScompared by addressaddress of error 1==address of error 2false: each erroris its own thing
The same two errors compared with ==, first as values and then as pointers.

The 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 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 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 , the length and the capacity. On a 64-bit machine that's 24 .
  • The backing array. This is the large block of memory where the tasks actually are.
POINTERl*ListSLICE HEADERptrlen: 4cap: 6BACKING ARRAYT0T1T2T3len 4cap 6
Dereferencing l copies the middle box and never the array on the right.

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.

FULLlen 4, cap 40149no room for a fifthcopied into a bigger arrayappend(s, 16)len 5, cap 8014916room for three more
append may move the data to a bigger array, and it always returns a new header.
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 . 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."

ltype *Listaddress of todo*List next to a type is the noun:pointer to Listtodotype ListA REAL LIST*lthe value at l&todothe address of todo
The asterisk in a type says what l holds. The asterisk in front of l goes to what it points at.

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 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.

LOOKS LIKEList* a, b;List*abtwo pointerswhat the spacing suggests,and not what it meansPARSES ASList (*a), (b);List*abpointerplain Listthe asterisk belongs to thename a, so b never gets one
Top, the reading that the spacing suggests. Bottom, the grouping the grammar uses.
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.

A round portrait of Rob Pike, with his name below.
Rob Pike, one of Go's three designers, speaking in 2010. Before Go, he worked at Bell Labs on Unix and on the Plan 9 operating system.Photo: Kevin Shockey, CC BY 2.0, via Wikimedia Commons (original). Changes: cropped to a circle and placed on a card with the name.

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
  1. 1.Why do the todo app's functions return *TaskError rather than TaskError?
  2. 2.In func (l *List) Add(task Task), why write *l = append(*l, task)?
  3. 3.In C or C++, what does List* a, b; declare?

Who wrote this

Author profile and background

Takumi Yamada

Takumi Yamada

· Backend Engineer

Backend engineer building services, databases, Linux tooling, and NEGEN SYSTEMS. Writing about low-level mechanics, distributed systems, and real-world security.

End of post
NEGEN SYSTEMS
Receipt · post 18 of 31
Written
Apr 14, 2026
Length
1,349 words · 6 min
Built
…
Served by
…
Delivered
…
Over the wire
…
Go's share
…
Next in Programming Languages 101 · part 6 of 6

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.

6 min read
From other series