Skip to main content

usage-patterns-and-examples

usage-patterns-and-examples

소스 정보

저장소
Goldziher/go-utils
최근 소스 활동
2026년 10월 14일 13:32
감지된 SKILL.md 언어
영어
스타
63
포크
16

설치 방법

기본적으로 소스를 먼저 확인하는 Prompt가 선택됩니다. 직접 명령으로 전환하거나 로컬 사본을 다운로드할 수도 있습니다.

소스 파일 검토

설치 여부를 결정하기 전에 SKILL.md와 SkillsMP에 표시된 보조 파일을 읽어 보세요.

SKILL.md 표시 중

SKILL.md
소스 지침 · 읽기 전용 미리보기
description
usage-patterns-and-examples
name
usage-patterns-and-examples
# Usage Patterns and Examples ## Usage Patterns and Examples ### Installation in Your Project ```bash go get -u github.com/Goldziher/go-utils ``` ### Common Usage Patterns #### Working with Slices (JavaScript-style) The `sliceutils` package provides familiar operations for developers coming from JavaScript: ```go import "github.com/Goldziher/go-utils/sliceutils" // Filter and Map (chainable pattern) numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} filtered := sliceutils.Filter(numbers, func(val int, idx int, slice []int) bool { return val%2 == 0 }) // [2, 4, 6, 8, 10] doubled := sliceutils.Map(filtered, func(val int, idx int, slice []int) int { return val * 2 }) // [4, 8, 12, 16, 20] // Reduce for aggregation sum := sliceutils.Reduce(numbers, func(acc int, cur int, idx int, slice []int) int { return acc + cur }, 0) // 55 // Set operations a := []int{1, 2, 3, 4} b := []int{3, 4, 5, 6} union := sliceutils.Union(a, b) // [1, 2, 3, 4, 5, 6] intersection := sliceutils.Intersection(a, b) // [3, 4] difference := sliceutils.Difference(a, b) // [1, 2, 5, 6] ``` #### Working with Maps ```go import "github.com/Goldziher/go-utils/maputils" data := map[string]int{"a": 1, "b": 2, "c": 3} // Extract keys and values keys := maputils.Keys(data) // ["a", "b", "c"] values := maputils.Values(data) // [1, 2, 3] // Filter map entries filtered := maputils.Filter(data, func(key string, value int) bool { return value > 1 }) // {"b": 2, "c": 3} // Merge maps (right-to-left precedence) m1 := map[string]int{"a": 1, "b": 2} m2 := map[string]int{"b": 3, "c": 4} merged := maputils.Merge(m1, m2) // {"a": 1, "b": 3, "c": 4} ``` #### Working with Structs ```go import "github.com/Goldziher/go-utils/structutils" type User struct { Name string `json:"name"` Age int `json:"age"` Email string `json:"email"` } user := User{Name: "Alice", Age: 30, Email: "alice@example.com"} // Convert struct to map (respects struct tags) userMap := structutils.ToMap(user, "json") // {"name": "Alice", "age": 30, "email": "alice@example.com"} ``` #### URL Query String Building ```go import "github.com/Goldziher/go-utils/urlutils" // From map params := map[string]any{ "search": "golang", "page": 1, "tags": []string{"generics", "utils"}, } query := urlutils.QueryStringifyMap(params) // "page=1&search=golang&tags=generics&tags=utils" // From struct type QueryParams struct { Search string `qs:"q"` Page int `qs:"page"` Tags []string `qs:"tag"` } params := QueryParams{Search: "golang", Page: 1, Tags: []string{"generics", "utils"}} query := urlutils.QueryStringifyStruct(params, "qs") // "page=1&q=golang&tag=generics&tag=utils" ``` ### Type Safety with Generics All functions provide compile-time type safety: ```go // Compile error: type mismatch numbers := []int{1, 2, 3} result := sliceutils.Map(numbers, func(val int, idx int, slice []int) string { return strconv.Itoa(val) // Returns []string, not []int }) ```
GitHub에서 보기