CompactFunc Dedupes Case Variants

Merge adjacent case-variant duplicates with slices.CompactFunc, using strings.EqualFold as the comparator so GET, get and Get collapse.

GoData Integrity

Code

gosnippet
21 lines
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

FAQ

How do you run CompactFunc Dedupes Case Variants?
Execute it straight away — the program needs nothing beyond the runtime.