From 3dee555c5cc26d406daa190da699fd0c3ec0ec81 Mon Sep 17 00:00:00 2001 From: John Costa Date: Mon, 24 Nov 2025 20:34:15 +0000 Subject: [PATCH] feat: adding gist section with pointer vs value gist --- content/gists/_index.md | 6 ++++ content/gists/pointer-vs-value.md | 49 +++++++++++++++++++++++++++++++ hugo.toml | 6 +++- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 content/gists/_index.md create mode 100644 content/gists/pointer-vs-value.md diff --git a/content/gists/_index.md b/content/gists/_index.md new file mode 100644 index 0000000..408772f --- /dev/null +++ b/content/gists/_index.md @@ -0,0 +1,6 @@ ++++ +title = 'Gists' +toc = true ++++ + +Small snippets, random thoughts, bits of code. Anything really. diff --git a/content/gists/pointer-vs-value.md b/content/gists/pointer-vs-value.md new file mode 100644 index 0000000..1df87b0 --- /dev/null +++ b/content/gists/pointer-vs-value.md @@ -0,0 +1,49 @@ ++++ +title = "Pointer vs Value" +date = "2025-11-24" +author = "John Costa" +tags = ["Software", "gist"] ++++ + +When working with pointers, what do you think the difference is? + +## 1. Assign to pointer +```go +func (c *MyStruct) SomeFunction(val any) { + parsedStruct := someProcessingFunc(val) + c = &parsedStruct +} +``` + +vs + +## 2. Assign to value +```go +func (c *MyStruct) SomeFunction(val any) { + parsedStruct := someProcessingFunc(val) + *c = parsedStruct +} +``` + +I came across this at work today and it took me a second to figure out what was wrong. + + +## Number 2 is correct + +The first example actually assigned the pointer of `parsedStruct` to the local variable `c` which itself is just a pointer. + +```go +c = &parsedStruct +``` + +All we've done, is reassign a local variable. + +Whereas: + +```go +*c = parsedStruct +``` + +Actually changes the underlying value of the pointer `c`, and changes it to the content of `parsedStruct`. + +It's easy to overlook these things, specially now when LLMs write more and more code. But it will be more and more important to understand and spot these differences, they might cost you or your company a lot of business. diff --git a/hugo.toml b/hugo.toml index 5dbd5ab..240c072 100644 --- a/hugo.toml +++ b/hugo.toml @@ -15,9 +15,13 @@ theme = 'risotto' name = 'Projects' pageRef = '/projects' [[menu.main]] - weight = 10 + weight = 20 name = 'Books & TV' pageRef = '/books-and-tv' +[[menu.main]] + weight = 10 + name = 'Gists' + pageRef = '/gists' [params.about] title = "John Costa"