Skip to content

Commit 7c58434

Browse files
committed
feat: kc->kutl & add xgo
Signed-off-by: MEX7 <mex7.0828@gmail.com>
1 parent 35024a3 commit 7c58434

File tree

10 files changed

+78
-8
lines changed

10 files changed

+78
-8
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,19 @@
77
Common Tool set based on go 1.18
88

99

10+
## Project Introduction
11+
12+
This project is a tool library project.
13+
14+
### /example
15+
16+
Example code for function calls under the PKG folder.
17+
18+
### /pkg
19+
20+
Parts that can be called in your projects.
21+
22+
### /test
23+
24+
A place where some feature or new feature is tested.
25+

pkg/kc/file.go renamed to pkg/kutl/file.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
package kc
1+
package kutl
22

33
import (
4+
"io/ioutil"
45
"log"
6+
"net/http"
57
"os"
68
"path/filepath"
79
"strings"
10+
"time"
811
)
912

1013
// GetCurrentDirectory ...
@@ -27,3 +30,21 @@ func IsFileExists(path string) (bool, error) {
2730
}
2831
return false, err
2932
}
33+
34+
func DownloadPictureBytes(url string, timeout time.Duration) (res []byte, err error) {
35+
client := http.Client{
36+
Timeout: timeout,
37+
}
38+
resp, err := client.Get(url)
39+
if err != nil {
40+
return
41+
}
42+
defer func() { _ = resp.Body.Close() }()
43+
fileByte := make([]byte, 0)
44+
fileByte, err = ioutil.ReadAll(resp.Body)
45+
if err != nil {
46+
return
47+
}
48+
return fileByte, nil
49+
50+
}

pkg/kc/float.go renamed to pkg/kutl/float.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kc
1+
package kutl
22

33
import (
44
"fmt"

pkg/kc/map.go renamed to pkg/kutl/map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kc
1+
package kutl
22

33
import (
44
"fmt"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kc
1+
package kutl
22

33
import (
44
"reflect"

pkg/kc/math.go renamed to pkg/kutl/math.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kc
1+
package kutl
22

33
import (
44
"bytes"

pkg/kc/slice.go renamed to pkg/kutl/slice.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kc
1+
package kutl
22

33
import (
44
"bytes"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kc
1+
package kutl
22

33
import (
44
"reflect"

pkg/kc/time.go renamed to pkg/kutl/time.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kc
1+
package kutl
22

33
import (
44
"fmt"

pkg/xgo/xgo.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package xgo
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"runtime"
7+
8+
"go.uber.org/zap"
9+
)
10+
11+
// Go goroutine
12+
func Go(fn func()) {
13+
go try2(fn, nil)
14+
}
15+
16+
func try2(fn func(), cleaner func()) (ret error) {
17+
if cleaner != nil {
18+
defer cleaner()
19+
}
20+
defer func() {
21+
if err := recover(); err != nil {
22+
_, file, line, _ := runtime.Caller(5)
23+
log.Print("recover", zap.Any("err", err), zap.String("line", fmt.Sprintf("%s:%d", file, line)))
24+
if _, ok := err.(error); ok {
25+
ret = err.(error)
26+
} else {
27+
ret = fmt.Errorf("%+v", err)
28+
}
29+
}
30+
}()
31+
fn()
32+
return nil
33+
}

0 commit comments

Comments
 (0)