|
5 | 5 | [](https://codecov.io/gh/appleboy/com) |
6 | 6 | [](https://goreportcard.com/report/github.com/appleboy/com) |
7 | 7 |
|
8 | | -This is an open source project for commonly used functions for the [Go programming language](https://golang.org/). |
| 8 | +**Common Functions** is an open source collection of utility functions designed to simplify and accelerate Go development. This library provides robust, reusable helpers for common programming tasks such as random value generation, array and slice manipulation, file operations, and data type conversions. By centralizing frequently needed logic, it helps Go developers write cleaner, more efficient, and maintainable code across a wide range of projects. |
9 | 9 |
|
10 | 10 | ## Feature |
11 | 11 |
|
12 | | -* [x] Random |
13 | | -* [x] Array |
14 | | -* [x] File |
15 | | -* [x] Convert |
16 | | - |
17 | | -## Benchmrk |
18 | | - |
19 | | -```sh |
20 | | -goos: linux |
21 | | -goarch: amd64 |
22 | | -pkg: github.com/appleboy/com/array |
23 | | -cpu: Intel(R) Xeon(R) Platinum 8370C CPU @ 2.80GHz |
24 | | -BenchmarkArrayInMap |
25 | | -BenchmarkArrayInMap-2 411962 8343 ns/op 5224 B/op 8 allocs/op |
26 | | -BenchmarkArrayInSlice |
27 | | -BenchmarkArrayInSlice-2 4165724 863.8 ns/op 0 B/op 0 allocs/op |
28 | | -BenchmarkIn |
29 | | -BenchmarkIn-2 4610620 776.3 ns/op 1792 B/op 1 allocs/op |
30 | | -BenchmarkInArray |
31 | | -BenchmarkInArray-2 388922 9177 ns/op 1624 B/op 101 allocs/op |
32 | | -PASS |
33 | | -ok github.com/appleboy/com/array 16.040s |
34 | | -goos: linux |
35 | | -goarch: amd64 |
36 | | -pkg: github.com/appleboy/com/convert |
37 | | -cpu: Intel(R) Xeon(R) Platinum 8370C CPU @ 2.80GHz |
38 | | -BenchmarkCountParamsOld |
39 | | -BenchmarkCountParamsOld-2 2575500 1400 ns/op 0 B/op 0 allocs/op |
40 | | -BenchmarkCountParamsNew |
41 | | -BenchmarkCountParamsNew-2 33431834 108.5 ns/op 0 B/op 0 allocs/op |
42 | | -BenchmarkBytesToStrOld01 |
| 12 | +- [x] Random |
| 13 | +- [x] Array |
| 14 | +- [x] File |
| 15 | +- [x] Convert |
| 16 | + |
| 17 | +## Package Usage |
| 18 | + |
| 19 | +### array |
| 20 | + |
| 21 | +Check if a value exists in a slice. |
| 22 | + |
| 23 | +```go |
| 24 | +import "github.com/appleboy/com/array" |
| 25 | + |
| 26 | +found := array.Contains([]int{1, 2, 3}, 2) // true |
| 27 | +``` |
| 28 | + |
| 29 | +### bytesconv |
| 30 | + |
| 31 | +Zero-allocation conversion between string and []byte. |
| 32 | + |
| 33 | +```go |
| 34 | +import "github.com/appleboy/com/bytesconv" |
| 35 | + |
| 36 | +b := bytesconv.StrToBytes("hello") |
| 37 | +s := bytesconv.BytesToStr([]byte{'w', 'o', 'r', 'l', 'd'}) |
| 38 | +``` |
| 39 | + |
| 40 | +### convert |
| 41 | + |
| 42 | +String case conversion, MD5 hashing, and float/byte conversion. |
| 43 | + |
| 44 | +```go |
| 45 | +import "github.com/appleboy/com/convert" |
| 46 | + |
| 47 | +snake := convert.SnakeCasedName("FooBar") // "foo_bar" |
| 48 | +title := convert.TitleCasedName("foo_bar") // "FooBar" |
| 49 | +hash := convert.MD5Hash("data") |
| 50 | +b := convert.Float64ToByte(3.14) |
| 51 | +f := convert.ByteToFloat64(b) |
| 52 | +``` |
| 53 | + |
| 54 | +### file |
| 55 | + |
| 56 | +File and directory utilities. |
| 57 | + |
| 58 | +```go |
| 59 | +import "github.com/appleboy/com/file" |
| 60 | + |
| 61 | +isDir, _ := file.IsDir("/tmp") |
| 62 | +isFile, _ := file.IsFile("/tmp/file.txt") |
| 63 | +_ = file.Copy("src.txt", "dst.txt") |
| 64 | +_ = file.Remove("/tmp/old") |
| 65 | +``` |
| 66 | + |
| 67 | +### gh |
| 68 | + |
| 69 | +Set GitHub Actions output variables. |
| 70 | + |
| 71 | +```go |
| 72 | +import "github.com/appleboy/com/gh" |
| 73 | + |
| 74 | +_ = gh.SetOutput(map[string]string{"key": "value"}) |
| 75 | +``` |
| 76 | + |
| 77 | +### random |
| 78 | + |
| 79 | +Generate random strings for various use cases. |
| 80 | + |
| 81 | +```go |
| 82 | +import "github.com/appleboy/com/random" |
| 83 | + |
| 84 | +s, _ := random.StringWithCharset(16, random.Alphanumeric) // secure random string |
| 85 | +fast := random.randStringBytesMaskImprSrcUnsafe(16) // fast, not secure |
| 86 | +``` |
| 87 | + |
| 88 | +### trace |
| 89 | + |
| 90 | +Measure and log function execution time. |
| 91 | + |
| 92 | +```go |
| 93 | +import "github.com/appleboy/com/trace" |
| 94 | + |
| 95 | +trace.ExecuteTime("myTask", func() { |
| 96 | + // code to measure |
| 97 | +}) |
43 | 98 | ``` |
0 commit comments