diff --git a/internal/goman/lib.go b/internal/goman/lib.go index fa0dbbb..0d0feaa 100644 --- a/internal/goman/lib.go +++ b/internal/goman/lib.go @@ -40,6 +40,34 @@ func New(f func()) { }() } +// NewWithArgs 创建带有参数的goroutine +func NewWithArgs(f func(args ...interface{}), args ...interface{}) { + _, file, line, _ := runtime.Caller(1) + + go func() { + locker.Lock() + instanceId++ + + var instance = &Instance{ + Id: instanceId, + CreatedTime: time.Now(), + } + + instance.File = file + instance.Line = line + + instanceMap[instanceId] = instance + locker.Unlock() + + // run function + f(args...) + + locker.Lock() + delete(instanceMap, instanceId) + locker.Unlock() + }() +} + // List 列出所有正在运行goroutine func List() []*Instance { locker.Lock() diff --git a/internal/goman/lib_test.go b/internal/goman/lib_test.go index 622a91f..11358d1 100644 --- a/internal/goman/lib_test.go +++ b/internal/goman/lib_test.go @@ -19,3 +19,10 @@ func TestNew(t *testing.T) { time.Sleep(1 * time.Second) } + +func TestNewWithArgs(t *testing.T) { + NewWithArgs(func(args ...interface{}) { + t.Log(args[0], args[1]) + }, 1, 2) + time.Sleep(1 * time.Second) +}