preslavrachev / preslav.me-comments

0 stars 0 forks source link

2024/04/23/two-reasons-to-prefer-struct-pointers-in-golang/ #30

Open utterances-bot opened 3 months ago

utterances-bot commented 3 months ago

The Two Reasons I Prefer Passing Struct Pointers Around · Preslav Rachev

I am a software engineer with a decade-long experience developing software in Python, Go, and Java.

https://preslav.me/2024/04/23/two-reasons-to-prefer-struct-pointers-in-golang/

jayzz55 commented 3 months ago

To say that the comparison is of 2 project value (without ID) returning true is logically wrong is misleading. Golang is using structural typing and that's just how structural typing works. There are other language like Java which does nominal typing and that becomes correct in your sense.

I think the argument made on comparing object without understanding how golang does its typing is misleading.

Furthermore, passing struct pointer makes it difficult and becomes an anti-pattern for writing concurrency within golang.

preslavrachev commented 2 months ago

@jayzz55 With all due repsect, I won't call Go a prime example of a language that features structural typing. Even if two struct types have identical field layouts, you need to do explicit type conversion if you want to assign a value of type A to type B.

type A {
    Foo string
}

type B {
    Foo string
}

a := A{Foo:"Bar"}
b := B(a)

You can also structurally match a typed value with one of an anonymous struct type with the same fields, but considering how ugly it is to declare and instantiate an anonymous struct type, it's even less likely to work in practice.

The only prime example of structural typing in Go are interfaces, however, if you have read my post on interfaces in Go, you'll see that I am not a fan of overusing them, either.

So, while in theory structural typing exists in Go, its real practical value is somewhat limited.