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: lo.ClonePtr #389

Open
rot1024 opened this issue Oct 31, 2023 · 1 comment
Open

Proposal: lo.ClonePtr #389

rot1024 opened this issue Oct 31, 2023 · 1 comment

Comments

@rot1024
Copy link

rot1024 commented Oct 31, 2023

Often I want to duplicate a pointer easily. It would be useful to have a function that makes this easy:

func ClonePtr[T any](t *T) *T {
	if t == nil {
		return nil
	}
	u := *t
	return &u
}

Example (cloning URL):

u := lo.Must(url.Parse("https://example.com"))

v := lo.ClonePtr[url.URL](u)
// u and v are *url.URL
@ivila
Copy link

ivila commented Nov 2, 2023

Maybe calling it ShallowCopy?
I think people need to be notified that the new instance share something(the reference field, like User field in URL) with the old one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants