Skip to content
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

Closed
sajjanjyothi opened this issue Sep 30, 2023 · 8 comments
Closed

proposal: strings: Create a string pointer from string constant #63309

sajjanjyothi opened this issue Sep 30, 2023 · 8 comments
Labels
Milestone

Comments

@sajjanjyothi
Copy link

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.

@gopherbot gopherbot added this to the Proposal milestone Sep 30, 2023
@sajjanjyothi sajjanjyothi changed the title proposal: affected/package: proposal: strings: Create a string pointer from string constant Sep 30, 2023
@robpike
Copy link
Contributor

robpike commented Sep 30, 2023

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.

func ptr(s string) *string { return &s }

https://go.dev/play/p/D60yCWbOuAN

Bonus: if you do it my way, the call site is much shorter and requires no external module.

@mvdan
Copy link
Member

mvdan commented Sep 30, 2023

FWIW we tend to write a generic helper in some packages, like:

func addr[T any](t T) *T { return &t }

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.

@Jorropo
Copy link
Member

Jorropo commented Sep 30, 2023

@mvdan then maybe new should accept constants and literals ?

@seankhliao
Copy link
Member

see #45624

@seankhliao
Copy link
Member

also #61082

@go101
Copy link

go101 commented Oct 1, 2023

emp: = Employee{
   Name: &[]string{"hello"}[0],
}

@sajjanjyothi
Copy link
Author

I think the generic implementation would be an ideal solution
func pointer[T any](v T) *T { return &v }

if we could accommodate in language itself as suggested part of std lib, would be easy for almost all go devs :)

@ianlancetaylor
Copy link
Contributor

Closing as a dup of #61082. Thanks.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Oct 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants