-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: strings: Create a string pointer from string constant #63309
Comments
There is no need for a language or library change; it's literally a one-liner, even in gofmt'ed Go. Just put this one-line func in your tests.
https://go.dev/play/p/D60yCWbOuAN Bonus: if you do it my way, the call site is much shorter and requires no external module. |
FWIW we tend to write a generic helper in some packages, like:
It is relatively common but I'm not even sure where this would belong in std, other than perhaps builtin, if not the language itself in some other form. |
@mvdan then maybe |
see #45624 |
also #61082 |
|
I think the generic implementation would be an ideal solution if we could accommodate in language itself as suggested part of std lib, would be easy for almost all go devs :) |
Closing as a dup of #61082. Thanks. |
When we want to pass a string pointer to a struct member each time we have to create a separate variable and pass the address of that to the member variable, Which is a pain. This wrapper function will ease all those efforts.
Eg:
type Employee struct{
Name *string
}
To pass a variable in testing , we have to
testName := "hello"
emp := Employee{
Name: &testName,
}
I have added a pull request to address this as #63299
With this function it is
emp: = Employee{
Name: strings.StringPointerFrom("hello")
}
It is really helpful when we are using table tests.
The text was updated successfully, but these errors were encountered: