In order to get a pointer that points to a string value.
You can't do something like:
&"my string"
Go 1.26 introduced this "new" function that does exactly that:
myPtr := new("my string")
Prior to that you could either write a helper function like in the screenshot or you could use a weird local temporary variable, which would make this entire thing really awkward to work with:
myString := "my string"
myPtr := &myString
Imagine you're trying to assign twenty string pointers to an object. Using this temporary local variable method, you'd first have to save these strings to twenty temporary variables only so you could use their pointers to assign them to the object. What a mess!
I'm glad Go 1.26 introduced this "new" function. But it really would have been helpful if it were part of the std libs many years ago.
Oh yeah, that was the worst part of the language for a while. Want to assign a nullable primitive field? Too bad, you gotta make a ton of single use constants. new() is a lifesaver
you'd first have to save these strings to twenty temporary variables
Perhaps I don't know how shadowing works in Go. Wouldn't
tmpString:="my first string"my1stPtr:=&tmpString
tmpString:="my second string"// Same name, different object
my2ndPtr:=&tmpString
work?
Yes, it is lots of different temporary variables, but they all have the same name. Go is going to have to collect the garbage either way.
I'm not scything that the separate function for doing this is worse than just reusing a variable name. At this point it is substantially a matter of taste as I understand it. I just to ask if I am correct that this can be done with a single temporary variable name.
But perhaps Golang doesn't like shadowing variable names (or perhaps good Go styling discourages it). I don't know.
In C++ this would be nasty because it would make a local copy of string because it’s passed by value, and then get a pointer to that local, and then the local would be destroyed when it went out of scope, returning a bad pointer.
I don’t know Go, but I’m guessing that A. passing a string like this is by reference like Java, and B. because it’s a garbage collected language, it won’t ever “go out of scope” until it’s not used anymore
Oh for sure, it works in Go, being a GC language, whether or not strings are passed by reference. I looked it up tho and strings are pass-by-value — but just the header, not the backing store. Interesting
Yes, that's escape analysis https://go.dev/doc/gc-guide#Escape_analysis - basically the compiler sees that a pointer is returned by a function, and decides to move the value from the stack to the heap, which makes it tracked by the garbage collector.
There was a deliberate choice in Go to call that "pointer" while C/C++ developers would understand pointer as just "address", so that can be counter-intuitive
Go is funny because that is never a concern. If you return a pointer to a “local”, the language itself moves that local to the heap so the pointer can remain alive. Heap usage is implicit.
In the context that it's used in, it does work. These values are assigned to a more complex object that works with pointers. These pointers are then used to be able to "nil" the complex object's values since they're optional. And we don't really care about where the pointer comes from as long as it points to a string with a certain value.
It's Go, and Go doesn't allow you to take the address of constants. Several popular libraries rely far too heavily on pointers to values that aren't necessarily variables, so having a function like this saves a little bit of friction. Rather than:
It's purely a style thing, because the compiler inlines the call to the "make pointer" function, and does a really good job of ensuring the allocations are on the stack whenever possible.
Huh yeah you might be right! Honestly I've thought of it more like "convert a POINTER TO a STRING" and not like "return a POINTER that points TO a STRING".
Depending on how you look at it, the meaning can be the opposite!
I guess whether or not this name is accurate depends on the conventions in your codebase. It made perfect sense to me, and does exactly what I thought it would do.
I remember a tweet/post from a long time ago that talked about showing a haskell function adding two numbers to people and asking them to find the bug, and then later pointing out that the function was called minus. There some errors from which no type system can save you =P
Anyway, why would anyone wrap something so basic with no extra stuff done, no side effects etc. in a function?
Because prior to 1.26, that was the way you had to do it if you wanted to get a pointer to a [string] literal. You could do it with local variables but then you'd have an extra variable lying around. I really like Go, it's my language of choice, but holy shit, the fact that this was the idiomatic way to get a pointer gives me an aneurysm.
Yeah, I love Go. I really do! But every now and then there are small things like this where I think "wtf why isn't this part of the standard libraries??" These are small things that I use daily. Yeah, I could write my own three line helper function but why do I have to? Every dev writes this exact helper function in every project ever. It's such a no-brainer to have this in the std libs.
And that's why almost every larger Go SDK has this exact same function as part of their lib so you don't have to write this nonsense yourself. Take AWS for example: https://docs.aws.amazon.com/sdk-for-go/api/aws/
strPtr = aws.String("my string")
This stuff should have been in the Go std lib from basically day one.
Yeah, I'm really glad I stepped away from AWS and went on to work more with vanilla Go and better open source libraries. What a relief! I really feel your pain though!
My company uses Go for almost everything back-end, and we finally got so fed up with AWS's Go library for the CDK that we moved to TypeScript for all CDK-related things.
It's annoying to have Node dependencies in Go projects just so you can do programmatic deployments, but it's less of a pain than using the Go CDK library. :(
I am convinced that go is just C syntax but reversed with concurrency. Like someone wanted people to use C, but tried to hide it by reversing the syntax.
But the thing is, C has consistent syntax.
Type name = data: Type functionName(input) name = functionName(input)
Like, int *arr[5]; what is your point? It’s still consistent right to left similar to assignment and to function definition and function call. You put the size on the right and are saying to return an int array.
Even if one thing wasn’t, the language is still pretty consistent in the way it’s typed and used for 99.99% of the lines.
This is still very consistent compared to other languages where sometimes you are calling a function, starting a lambda expression, mixing input and output positions, using yield or filters in the middle, ..etc.
You can declare things in c in one or two places. You don’t have to pepper the code with pointers looking at pointers looking at addresses looking at function pointers.
You also don’t have to pepper the code with bit fields and bit operations.
The number of inconsistencies and in my opinion unreadable code in other languages is much larger than c.
I mainly used Java then C# in my career because I could make organised structured maintainable c style code with classes, concurrency, and parallelism. C for embedded projects.
The go snippet mentioned had inconsistency between calling and defining and even in the function definition, some things needed to be read backwards compared to the rest of the line.
I have seen in python, JavaScript and other languages people going out of their way, making unmaintainable code, and sometimes these are the only tools available to the programmer or engineer, not to mention bugs in the language making things less consistent.
It's a quirk of the way Go handles constant values at compile time, sadly.
Fortunately, as another comment pointed out it's no longer necessary as of Go 1.26. There's still a little more boilerplate than necessary, but to get a pointer to a constant value now you can:
I can honestly only recommend Go! I come from Java and I'm really really glad that I don't have to deal with that language anymore. Not even touching Java with a long stick again. :)
Naah, VS Code is alright but I just can't stand mouse operated editors these days :) I've used VS Code for a while and it's alright because it's free. My workplace gave me an IntelliJ / Goland license and yeaah they have more features but meh. Operating them is too much effort and too little gain.
I use nvim or more specifically LazyVim these days. It's honestly all I ever needed! Comes with a linter, debugger, test runner and what not. As a Go dev I feel like you simply don't need more than that! It's really easy to install all of the plugins that you need. Configuring them is a bit of a pain though since you have to figure out how to do that in Lua.
I've played around with Helix Editor a lot in the past weeks. I love it and I would love to use it way more! It's a "batteries included" editor. Simply start it and use it. You don't have to mess around with plugins because everything's already there. However, you can really notice the community is smaller. "Standard" features take a good while to be implemented and unfortunately their debugger is so WIP that it's unusable at this stage. So unfortunately I had to go back to nvim. But once the debugger is usable, I'll ditch nvim in a heartbeat!
51
u/jpgoldberg Apr 09 '26
In addition to the ambiguous name, why does this function even exist
48
u/Temporary-Estate4615 Apr 09 '26
Why the hell would you even write this function?
8
u/justjanne Apr 09 '26
Honestly, that naming may be fine, just ambiguous?
After all, it's a function that creates a pointer that's pointing to a string.
2
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Apr 09 '26
Does & in Go work like it dose in C and C++? Does that just return a pointer to its argument?
3
u/NeverYelling Apr 09 '26
We have those all over our huge monolithic codebase. I hate it
2
u/kintar1900 Apr 09 '26
I guess whether or not this name is accurate depends on the conventions in your codebase. It made perfect sense to me, and does exactly what I thought it would do.
2
u/tonnynerd Apr 09 '26
I remember a tweet/post from a long time ago that talked about showing a haskell function adding two numbers to people and asking them to find the bug, and then later pointing out that the function was called minus. There some errors from which no type system can save you =P
2
u/SpikeyWallaby Apr 11 '26
It's not "convert a pointer to a string", its "this function returns a pointer to a string", which is correct. I tend to spell it
ref:1
u/IllResponsibility671 Apr 09 '26
I once reviewed a React component where the developer put a function they named componentDidMount inside a useEffect and I almost had a heart attack.
0
u/[deleted] Apr 09 '26
[deleted] — view deleted comment
26
u/[deleted] Apr 09 '26
[removed] — view removed comment
2
u/[deleted] Apr 09 '26
[removed] — view removed comment