Phil Pearl's Blog

submited by
Style Pass
2024-08-31 00:00:08

I seem to be writing about one blog post a year at the moment. I always tell myself I want to write more, but it doesn’t happen. This, however, is the first post in a series. But I only have two ideas for posts, and one seems like a bad idea, so it could be a short series!

Every now and again I visit the Go slack, in particular the #performance channel and the #darkarts channel. I almost never say anything.

Recently someone brought up the question of whether they could put non-pointer values in unsafe.Pointer variables. The general response was “no, that’s a bad idea”. I agree, it seems like a bad idea.

But if we never explore bad ideas we’ll never … er, actually if we never explore bad ideas we’ll be absolutely fine.

So why is this a bad idea? The main thought is that you’ll perhaps crash Go’s garbage collector. The Go GC looks at every pointer visible to the program to see what memory is still in use and what memory can be freed. If it follows a pointer that’s not pointing to a valid memory address, it could crash.

Leave a Comment