iter.go 1.0 KB

123456789101112131415161718192021222324252627282930
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package iter provides a syntactically different way to iterate over
  5. // integers. That's it.
  6. //
  7. // This package was intended to be an educational joke when it was
  8. // released in 2014. People didn't get the joke part and started
  9. // depending on it. That's fine, I guess. (This is the Internet.) But
  10. // it's kinda weird. It's one line, and not even idiomatic Go style. I
  11. // encourage you not to depend on this or write code like this, but I
  12. // do encourage you to read the code and think about the
  13. // representation of Go slices and why it doesn't allocate.
  14. package iter
  15. // N returns a slice of n 0-sized elements, suitable for ranging over.
  16. //
  17. // For example:
  18. //
  19. // for i := range iter.N(10) {
  20. // fmt.Println(i)
  21. // }
  22. //
  23. // ... will print 0 to 9, inclusive.
  24. //
  25. // It does not cause any allocations.
  26. func N(n int) []struct{} {
  27. return make([]struct{}, n)
  28. }