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

At about Go 1.26, add a SliceElemPointers function #1

Open
zigo101 opened this issue Sep 12, 2024 · 1 comment
Open

At about Go 1.26, add a SliceElemPointers function #1

zigo101 opened this issue Sep 12, 2024 · 1 comment

Comments

@zigo101
Copy link
Collaborator

zigo101 commented Sep 12, 2024

see: golang/go#69345

@zigo101
Copy link
Collaborator Author

zigo101 commented Sep 12, 2024

//go:build go1.23

package main

import "fmt"

func SliceElemPointers[E any](s []E) func(func(*E) bool) {
	return func(yield func(*E) bool) {
		for i := range s {
			if !yield(&s[i]) {
				break
			}
		}
	}
}

func main() {
	var x = []int{1, 3, 6}
	for p := range SliceElemPointers(x) {
		*p *= 2
	}
	fmt.Println(x) // [2 6 12]
}

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

1 participant