Skip to content

Commit 9de2561

Browse files
committed
feat: add GitHub Actions workflow for Go CI and remove unused byteutil package
1 parent d4fbcf8 commit 9de2561

File tree

7 files changed

+56
-90
lines changed

7 files changed

+56
-90
lines changed

.github/workflows/go.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Go
2+
on: [push]
3+
4+
permissions:
5+
contents: read
6+
7+
jobs:
8+
build:
9+
name: Build
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: actions/setup-go@v5
14+
with:
15+
go-version: stable
16+
17+
- name: golangci-lint
18+
uses: golangci/golangci-lint-action@v6
19+
with:
20+
version: v1.60
21+
22+
- name: Run golangci-lint
23+
run: golangci-lint run
24+
25+
- name: Test
26+
#run: echo go test -v .
27+
run: echo go test ./... -count=1 -v

byteutil/byte.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

byteutil/random_string.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

utilkit/byte.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package utilkit
33
// Refer: https://gist.github.com/yakuter/c0df0f4253ea639529f3589e99dc940b
44

55
import (
6-
"reflect"
76
"unsafe"
87
)
98

@@ -16,18 +15,15 @@ func BytesToString(b []byte) string {
1615
return *(*string)(unsafe.Pointer(&b))
1716
}
1817

19-
// s2b converts string to a byte slice without memory allocation.
18+
// StringToBytes converts a string to a byte slice without memory allocation.
2019
//
21-
// Note it may break if string and/or slice header will change
22-
// in the future go versions.
23-
func StringToBytes(s string) (b []byte) {
24-
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
25-
26-
//nolint: govet
27-
sh := *(*reflect.StringHeader)(unsafe.Pointer(&s))
28-
bh.Data = sh.Data
29-
bh.Len = sh.Len
30-
bh.Cap = sh.Len
20+
// Warning: This function is unsafe and should be used with caution. Modifying
21+
// the resulting byte slice may lead to undefined behavior if the original
22+
// string is immutable. Ensure compatibility with future Go versions.
23+
func StringToBytes(s string) []byte {
24+
// Get the pointer to the string data
25+
data := unsafe.StringData(s)
3126

32-
return b
27+
// Convert to a byte slice using unsafe.Slice
28+
return unsafe.Slice(data, len(s))
3329
}

utilkit/file.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package utilkit
33
import (
44
"archive/tar"
55
"io"
6-
"io/ioutil"
76
"log"
87
"os"
98
"path/filepath"
@@ -17,7 +16,6 @@ const (
1716

1817
func Exists(path string) bool {
1918
_, err := os.Stat(path)
20-
2119
return !os.IsNotExist(err)
2220
}
2321

@@ -39,14 +37,15 @@ func EnsureFileExists(path string) {
3937

4038
EnsureFolderExists(filepath.Dir(path))
4139

42-
err := ioutil.WriteFile(path, nil, FilePerm644)
40+
file, err := os.Create(path)
4341
if err != nil {
4442
log.Fatal("file not exists, err: ", err)
4543
}
44+
file.Close()
4645
}
4746

4847
func ReadFile(path string) ([]byte, error) {
49-
content, err := ioutil.ReadFile(path)
48+
content, err := os.ReadFile(path)
5049
if err != nil {
5150
log.Fatal("read file fail, err: ", err)
5251
return nil, err
@@ -58,7 +57,7 @@ func ReadFile(path string) ([]byte, error) {
5857
func WriteFile(path string, content string) error {
5958
EnsureFileExists(path)
6059

61-
err := ioutil.WriteFile(path, StringToBytes(content), FilePerm644)
60+
err := os.WriteFile(path, []byte(content), FilePerm644)
6261
if err != nil {
6362
return err
6463
}

utilkit/template.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ package utilkit
33
import (
44
"bytes"
55
"io"
6-
"io/ioutil"
76
"text/template"
8-
9-
"github.com/airdb/toolbox/byteutil"
107
)
118

129
func TemplateGenerateString(str string, data interface{}) (string, error) {
@@ -24,12 +21,12 @@ func TemplateGenerateString(str string, data interface{}) (string, error) {
2421
}
2522

2623
func TemplateGenerateFileFromReader(reader io.Reader, dstPath string, data interface{}) error {
27-
b, err := ioutil.ReadAll(reader)
24+
b, err := io.ReadAll(reader)
2825
if err != nil {
2926
return err
3027
}
3128

32-
content, err := TemplateGenerateString(byteutil.BytesToString(b), data)
29+
content, err := TemplateGenerateString(BytesToString(b), data)
3330
if err != nil {
3431
return err
3532
}

utilkit/time_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
package utilkit_test
22

33
import (
4+
"strings"
45
"testing"
56
"time"
67

78
"github.com/airdb/toolbox/utilkit"
89
)
910

1011
func TestGetNowLong(t *testing.T) {
11-
utilkit.GetNowLong()
12+
ts := utilkit.GetNowLong()
13+
if ts == "" {
14+
t.Errorf("Expected a valid timestamp, got %s", ts)
15+
}
1216
}
1317

1418
func TestGetYearMonthDay(t *testing.T) {
15-
utilkit.GetYearMonthDay()
19+
today := utilkit.GetYearMonthDay()
20+
if !strings.Contains(today, "-") {
21+
t.Errorf("Expected a valid date string, got %s", today)
22+
}
23+
1624
}
1725

1826
func TestGetTimeRFC(t *testing.T) {
1927
now := time.Now().Unix()
20-
utilkit.GetTimeRFC(now)
28+
rfcTime := utilkit.GetTimeRFC(now)
29+
if _, err := time.Parse(time.RFC3339, rfcTime); err != nil {
30+
t.Errorf("Expected valid RFC3339 time format, got %s", rfcTime)
31+
}
2132
}

0 commit comments

Comments
 (0)