init
This commit is contained in:
40
utils/file.go
Normal file
40
utils/file.go
Normal file
@ -0,0 +1,40 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
func ReadFile(path string) (string, error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
}
|
||||
defer file.Close()
|
||||
d, _ := io.ReadAll(file)
|
||||
return string(d), nil
|
||||
}
|
||||
|
||||
func WriteFile(path string, content string) error {
|
||||
file, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
file.WriteString(content)
|
||||
return nil
|
||||
}
|
||||
|
||||
func FormatBytes(bytes uint64) string {
|
||||
const unit = 1024
|
||||
if bytes < unit {
|
||||
return fmt.Sprintf("%d B", bytes)
|
||||
}
|
||||
div, exp := uint64(unit), 0
|
||||
for n := bytes / unit; n >= unit; n /= unit {
|
||||
div *= unit
|
||||
exp++
|
||||
}
|
||||
return fmt.Sprintf("%.1f %ciB", float64(bytes)/float64(div), "KMGTPE"[exp])
|
||||
}
|
27
utils/goroutine_pool.go
Normal file
27
utils/goroutine_pool.go
Normal file
@ -0,0 +1,27 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/panjf2000/ants"
|
||||
)
|
||||
|
||||
type PoolInfo struct {
|
||||
Pool *ants.Pool
|
||||
MaxWorkers int
|
||||
TaskWaitGroup sync.WaitGroup
|
||||
}
|
||||
|
||||
func (pool *PoolInfo) NewPool(num int) {
|
||||
p, err := ants.NewPool(num)
|
||||
if err != nil {
|
||||
fmt.Println("Create Pool Error:", err)
|
||||
return
|
||||
}
|
||||
pool.Pool = p
|
||||
pool.MaxWorkers = num
|
||||
}
|
||||
func (pool *PoolInfo) AddTask(fun func()) {
|
||||
pool.Pool.Submit(fun)
|
||||
}
|
Reference in New Issue
Block a user