4 ms·
Not being able to take a pointer to a literal is one of my pet peeves with Go - especially when using the AWS SDK, which requires pointers to literals everywher
by jen20 2y ago
Not being able to take a pointer to a literal is one of my pet peeves with Go - especially when using the AWS SDK, which requires pointers to literals everywhere. At least with generics, a wrapper function doesn't require a separate function per type though, and can be simply:
package ptr
func To[T any](v T) *T {
return &v
}
- kbolino 2y agoYeah, that particular case got better with generics. It's still more verbose to write ptr.To(true) than &true though. Unfortunately, there's no generic type constraint for "is an interface" or even "is nilable" so you can't use generics to solve nil-safety issues in general.
- jen20 2y agoIndeed, I see no reason why taking the address of a literal couldn’t be added though, that’s a cheap win.
- deergomoo 2y ago> especially when using the AWS SDK, which requires pointers to literals everywhere On the plus side, at least the AWS SDK provides `aws.String()` and friends.
- jen20 2y agoAt least they did _something_, but it would be better if they moved into the modern age of Go and provided the version above as well.
- anothername12 2y agolol We have this function implemented at least a dozen times through our mono repo. All named slightly different.