CompactFunc Dedupes Case Variants
Merge adjacent case-variant duplicates with slices.CompactFunc, using strings.EqualFold as the comparator so GET, get and Get collapse.
Code
package main
import (
"fmt"
"slices"
"strings"
)
func main() {
// 相邻的等值(忽略大小写)变体只保留第一个
logs := []string{"GET", "get", "Get", "POST", "PUT", "put", "DELETE"}
compact := slices.CompactFunc(logs, func(a, b string) bool {
return strings.EqualFold(a, b)
})
fmt.Println(compact)
// CompactFunc 只看相邻元素:不相邻的变体不会被合并
scattered := []string{"get", "POST", "GET"}
fmt.Println(slices.CompactFunc(scattered, strings.EqualFold))
}How the snippet works
Under the hood: the function main.
Points of care
- Only built-in APIs appear, leaving no supply chain to review.
Using it
The line that defines the shape: func main()
Key statements
return strings.EqualFold(a, b)
Interactive Tools for This Topic
Case ConverterConvert text between lowercase, uppercase, title, sentence, camelCase, snake_case, and kebab-case while preserving Unicode letters and numbers.cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- How do you run CompactFunc Dedupes Case Variants?
- Execute it straight away — the program needs nothing beyond the runtime.
Report a problem with this exampleOpens a pre-filled issue on GitHub.