Reusable utility helpers for Go applications, including generic collection helpers, reflection helpers, JSON/YAML conversion, HTTP helpers, Kubernetes wrappers, and lightweight data structures.
- Go 1.24+
go get github.com/RamanPndy/go-utilsThen import the package:
import goutils "github.com/RamanPndy/go-utils/utils"Map,Filter,Any,AllContains,Dedupe,Zip,CombineSlicesToMapEquals,EqualsSlice,EqualsMap
NewSet,NewFrozenSet,NewOrderedSetNewImmutableDict,NewOrderedDictNewIterator,NewOrderedList
HasAttr,SetAttr,VarsIsInstance,IsSubclass,IsNilInterfaceGetStructFieldNames,GetStructFieldValueMergeUniqueFields,SkipMergeUniqueFields
DeepCopyJSON,MustJSONJsonEncode*andJsonDecode*helpersJsonToYaml*andYamlToJson*JSONEqual,JSONDeepEqual,JSONContainsBase64Encode,Base64DecodeEscapeJSONPointer,UnescapeJSONPointer
NewAPIClient,NewClientWithToken,NewAPIRequest- URL/query helpers:
EncodeQueryParams,DecodeQueryParams - Validation helpers:
IsValidURL,IsValidStatusCode,IsValidContentType, and related validators GetContentTypeFromHeaders,IsValidAPIResult
- Database and DSN:
BuildDSN,NewDBConn,OpenDB,NewDsnLoader,NewPostgresStore - Cache:
NewCache,NewMemoryCache,NewMultiCache,NewRedisCacheFromGoRedis,NewMemcacheCacheFromClient - File watching:
NewFileWatcher,NewWatcher,NotifyOnFileChange - Kubernetes command wrappers:
KubectlGet,KubectlApply,KubectlRaw,InClusterConfig - Misc:
ReadFile,Compute,UnixTimeToTimestamp,GetFirstNonEmpty,IndentBlock
nums := []int{1, 2, 3, 4}
squares := goutils.Map(nums, func(n int) int { return n * n })
evens := goutils.Filter(nums, func(n int) bool { return n%2 == 0 })type User struct {
Name string
Age int
}
u := User{Name: "Alice", Age: 30}
hasName := goutils.HasAttr(u, "Name")
_ = hasName
name, _ := goutils.GetStructFieldValue(u, "Name")
_ = nametype Payload struct {
ID int `json:"id"`
Name string `json:"name"`
}
data := Payload{ID: 1, Name: "demo"}
jsonStr, _ := goutils.JsonEncodeToString(data)
var decoded Payload
_ = goutils.JsonDecodeFromString(jsonStr, &decoded)
_ = decodedctx := context.Background()
cache := goutils.NewMemoryCache()
_ = cache.Set(ctx, "user:1", []byte("Alice"), 5*time.Minute)
value, _ := cache.Get(ctx, "user:1")
_ = valueredisClient := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"})
redisCache, _ := goutils.NewRedisCacheFromGoRedis(redisClient)
_ = redisCache.Set(context.Background(), "session:token", []byte("abc123"), 10*time.Minute)memc := memcache.New("127.0.0.1:11211")
memcacheCache, _ := goutils.NewMemcacheCacheFromClient(memc)
_ = memcacheCache.Set(context.Background(), "feature:flag", []byte("enabled"), time.Minute)primary := goutils.NewMemoryCache()
fallback := goutils.NewMemoryCache()
multi, _ := goutils.NewMultiCache(primary, fallback)
_ = multi.Set(context.Background(), "key", []byte("value"), 0)The scripts/ directory contains Kubernetes helper scripts:
scripts/k8s-cpu-memory.sh <namespace>- Prints deployment/container CPU and memory requests/limits.
scripts/k8s-get-image.sh- Interactive script to select namespace, deployment, and container, then display the current image.
scripts/k8s-set-image.sh- Interactive script to update a selected container image in a deployment.
These scripts require kubectl configured for your cluster. k8s-cpu-memory.sh also requires jq.
go test ./...The repository includes a sample main that exercises many helpers:
go run .MIT (see LICENSE).