Skip to Content

Utils

The utils package provides a collection of general-purpose utility functions including pagination math, string manipulation, slice operations, and other common helpers used throughout Gofasta applications.

Import

import "github.com/gofastadev/gofasta/pkg/utils"

Key Functions

Pagination

FunctionSignatureDescription
CalcOffsetfunc CalcOffset(page, pageSize int) intCalculates the database offset from page and page size
CalcTotalPagesfunc CalcTotalPages(total int64, pageSize int) intCalculates the total number of pages
ClampPagefunc ClampPage(page, totalPages int) intClamps a page number to valid range

String Helpers

FunctionSignatureDescription
Slugifyfunc Slugify(s string) stringConverts a string to a URL-friendly slug
Truncatefunc Truncate(s string, maxLen int) stringTruncates a string to the given length with ellipsis
CamelToSnakefunc CamelToSnake(s string) stringConverts CamelCase to snake_case
SnakeToCamelfunc SnakeToCamel(s string) stringConverts snake_case to CamelCase
Pluralizefunc Pluralize(s string) stringReturns the plural form of a word
RandomStringfunc RandomString(n int) stringGenerates a random alphanumeric string of length n

Slice Utilities

FunctionSignatureDescription
Containsfunc Contains[T comparable](slice []T, item T) boolChecks if a slice contains an item
Uniquefunc Unique[T comparable](slice []T) []TReturns a slice with duplicates removed
Mapfunc Map[T any, U any](slice []T, fn func(T) U) []UTransforms each element of a slice
Filterfunc Filter[T any](slice []T, fn func(T) bool) []TReturns elements that match the predicate
Chunkfunc Chunk[T any](slice []T, size int) [][]TSplits a slice into chunks of the given size
Flattenfunc Flatten[T any](slices [][]T) []TFlattens a slice of slices into a single slice
IndexOffunc IndexOf[T comparable](slice []T, item T) intReturns the index of an item, or -1

Map Utilities

FunctionSignatureDescription
Keysfunc Keys[K comparable, V any](m map[K]V) []KReturns all keys of a map
Valuesfunc Values[K comparable, V any](m map[K]V) []VReturns all values of a map
MergeMapsfunc MergeMaps[K comparable, V any](maps ...map[K]V) map[K]VMerges multiple maps, last write wins

Pointer Helpers

FunctionSignatureDescription
Ptrfunc Ptr[T any](v T) *TReturns a pointer to the given value
Dereffunc Deref[T any](p *T, fallback T) TDereferences a pointer with a fallback for nil

Usage

Pagination Calculations

offset := utils.CalcOffset(3, 20) // 40 totalPages := utils.CalcTotalPages(95, 20) // 5 page := utils.ClampPage(10, 5) // 5

String Operations

slug := utils.Slugify("Hello World! This is a Test") // -> "hello-world-this-is-a-test" truncated := utils.Truncate("This is a very long string that should be cut off", 20) // -> "This is a very lo..." snake := utils.CamelToSnake("UserProfileSettings") // -> "user_profile_settings" camel := utils.SnakeToCamel("user_profile_settings") // -> "UserProfileSettings"

Slice Operations

names := []string{"alice", "bob", "charlie", "alice"} utils.Contains(names, "bob") // true utils.Unique(names) // ["alice", "bob", "charlie"] utils.IndexOf(names, "charlie") // 2 numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} evens := utils.Filter(numbers, func(n int) bool { return n%2 == 0 }) // -> [2, 4, 6, 8, 10] doubled := utils.Map(numbers, func(n int) int { return n * 2 }) // -> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] chunks := utils.Chunk(numbers, 3) // -> [[1,2,3], [4,5,6], [7,8,9], [10]]

Pointer Helpers

name := utils.Ptr("Jane") // *string pointing to "Jane" value := utils.Deref(name, "") // "Jane" value = utils.Deref(nil, "default") // "default"

Map Operations

m := map[string]int{"a": 1, "b": 2, "c": 3} keys := utils.Keys(m) // ["a", "b", "c"] values := utils.Values(m) // [1, 2, 3] merged := utils.MergeMaps( map[string]int{"a": 1, "b": 2}, map[string]int{"b": 3, "c": 4}, ) // -> {"a": 1, "b": 3, "c": 4}
Last updated on