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.
Check if a value exists in a slice.
import "github.com/appleboy/com/array"
found := array.Contains([]int{1, 2, 3}, 2) // true
Zero-allocation conversion between string and []byte.
import "github.com/appleboy/com/bytesconv"
b := bytesconv.StrToBytes("hello")
s := bytesconv.BytesToStr([]byte{'w', 'o', 'r', 'l', 'd'})
String case conversion, MD5 hashing, and float/byte conversion.
import "github.com/appleboy/com/convert"
snake := convert.SnakeCasedName("FooBar") // "foo_bar"
title := convert.TitleCasedName("foo_bar") // "FooBar"
hash := convert.MD5Hash("data")
b := convert.Float64ToByte(3.14)
f := convert.ByteToFloat64(b)
File and directory utilities.
import "github.com/appleboy/com/file"
isDir, _ := file.IsDir("/tmp")
isFile, _ := file.IsFile("/tmp/file.txt")
_ = file.Copy("src.txt", "dst.txt")
_ = file.Remove("/tmp/old")
Set GitHub Actions output variables.
import "github.com/appleboy/com/gh"
_ = gh.SetOutput(map[string]string{"key": "value"})
Generate random strings for various use cases.
import "github.com/appleboy/com/random"
s, _ := random.StringWithCharset(16, random.Alphanumeric) // secure random string
fast := random.randStringBytesMaskImprSrcUnsafe(16) // fast, not secure
Measure and log function execution time.
import "github.com/appleboy/com/trace"
trace.ExecuteTime("myTask", func() {
// code to measure
})