loop

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2025 License: MIT Imports: 0 Imported by: 1

README

go-loop 🔁

go-loop 是一个简单又实用的 Go 工具包,用来让你的循环变得更优雅、更 ✨函数式✨。
它专门用来解决在标准 for 循环中,defer 只能在整个函数退出时才执行的问题!

🚀 安装

go get github.com/lvan100/go-loop

🛠️ 使用指南

🔂 Times

Times 函数执行一个回调函数指定的次数。

loop.Times(5, func (i int) {
    fmt.Println(i) // 输出 0 到 4
})
📈 Ranges

Rangesstartend(不包含 end)进行遍历。支持正向和反向!

loop.Ranges(2, 5, func (i int) {
    fmt.Println(i) // 输出 2, 3, 4
})

loop.Ranges(5, 2, func (i int) {
    fmt.Println(i) // 输出 5, 4, 3
})
🏃 StepRanges

StepRanges 允许你自定义步长,灵活控制每次迭代的间隔。正着走也行,倒着走也行!

loop.StepRanges(0, 10, 2, func(i int) {
    fmt.Println(i) // 输出 0, 2, 4, 6, 8
})

loop.StepRanges(10, 0, -3, func (i int) {
    fmt.Println(i) // 输出 10, 7, 4, 1
})

⚡ 为什么需要它?

在传统 for 循环中写 defer,所有延迟操作都会在函数返回时才统一执行,而不是在每次循环迭代时执行。
使用 go-loop,可以通过闭包手动控制作用域,让每次循环中的 defer 在预期时机生效!🎯

示例:

loop.Times(3, func (i int) {
    defer fmt.Println("deferred", i)
    fmt.Println("running", i)
})

输出:

running 0
deferred 0
running 1
deferred 1
running 2
deferred 2

📄 许可证

本项目遵循 MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ranges

func Ranges(start, end int, fn func(i int))

Ranges iterates from 'start' to 'end' (exclusive) and applies 'fn' to each index. Only used to eliminate deferred execution under standard for loops.

func StepRanges

func StepRanges(start, end, step int, fn func(i int))

StepRanges iterates from 'start' to 'end' using a step size and applies 'fn' to each index. Only used to eliminate deferred execution under standard for loops.

func Times

func Times(count int, fn func(i int))

Times executes the function 'fn' exactly 'count' times. Only used to eliminate deferred execution under standard for loops.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL