Skip to content

πŸ”– Function ​

go
import (
	"time"
	"github.com/thuongtruong109/gouse"
)

1. Function delay ​

Description: Delay function execution for a specific time.
Input params: (func() string, delay int)

go
func FunctionDelay() {
	println("Delay start:")

	result := gouse.DelayF(func() string {
		return "Delayed return after 2s"
	}, 2)

	if result.HasReturn {
		println(result.Value)
	} else {
		println("No result")
	}

	gouse.DelayFunc(func() {
		println("Delayed not return")
	}, 3)
}

2. Function interval ​

Description: Execute function at a specific interval.
Input params: (func(), interval int)

go
func FunctionInterval() {
	gouse.IntervalFunc(func() {
		println("Interval")
	}, 1)
}

3. Function lock ​

Description: Lock function execution.

go
func FunctionLock() {
	oneInOneOutLockFunc := gouse.LockFunc(func(i interface{}) interface{} {
		return i
	}).(func(interface{}) interface{})("one input - one output")
	gouse.Println(oneInOneOutLockFunc)

	twoInTwoOutLockFunc1, twoInTwoOutLockFunc2 := gouse.LockFunc(func(i1, i2 interface{}) (interface{}, interface{}) {
		return i1, i2
	}).(func(interface{}, interface{}) (interface{}, interface{}))("two input - two output (a)", "two input - two output (b)")
	gouse.Println(twoInTwoOutLockFunc1, twoInTwoOutLockFunc2)

	gouse.LockFunc(func() {
		println("no input - no output")
	}).(func())()

	exampleRWLockFunc := func(a, b int) int {
		return a + b
	}

	lockedFunc := gouse.RWLockFunc(exampleRWLockFunc).(func(int, int) int)
	result := lockedFunc(5, 3)
	gouse.Println("RW Lock function result:", result)
}

4. Function parallel ​

Description: Parallelize multi functions execution.
Input params: (...func())

go
func FunctionParallel() {
	function1 := func() {
		for i := 0; i < 5; i++ {
			time.Sleep(100 * time.Millisecond)
			gouse.Println("Function 1 is executing")
		}
	}

	function2 := func() {
		for i := 0; i < 5; i++ {
			time.Sleep(200 * time.Millisecond)
			gouse.Println("Function 2 is executing")
		}
	}

	function3 := func() {
		for i := 0; i < 5; i++ {
			time.Sleep(300 * time.Millisecond)
			gouse.Println("Function 3 is executing")
		}
	}

	gouse.ParallelizeFunc(function1, function2, function3)
}

5. Function remain ​

Description: Limit function execution.
Input params: (func(), times int)

go
func FunctionRemain() {
	gouse.RemainFunc(func() {
		println("Times")
	}, 3)
}

6. Function retry ​

Description: Retry function execution.
Input params: (func(), times int, delay int)

go
func FunctionRetry() {
	gouse.RetryFunc(func() error {
		println("Retry")
		return nil
	}, 3, 1)
}

7. Function run time ​

Description: Measure function execution time.
Input params: (time.Time, func())

go
func FunctionRunTime() {
	exampleFunc := func() {
		time.Sleep(2 * time.Second)
		gouse.Println("Task completed.")
	}

	duration := gouse.RunTimeFunc(time.Now(), exampleFunc)
	gouse.Printf("Function run in: %v\n", duration)
}

8. Func defer wrapper ​

Description: Wrap defer function
Input params: (...func())

go
func FuncDeferWrapper() {
	gouse.DeferWrapper(
		func() error {
			gouse.Println("Connecting to database...")
			return nil
		},
		func() {
			gouse.Println("Disconnecting from database...")
		},
	)
}

Released under the MIT License.