Bytes EqualFold ASCII Case

Compare byte slices case-insensitively with bytes.EqualFold, including a Unicode simple-fold pair that plain ASCII lowering would miss.

GoData Integrity

Code

gosnippet
25 lines
package main

import (
	"bytes"
	"fmt"
)

func main() {
	// EqualFold does Unicode simple folding, not a ToLower comparison
	fmt.Println("ascii fold:", bytes.EqualFold([]byte("GoLang"), []byte("golang")))
	fmt.Println("length differs:", bytes.EqualFold([]byte("go"), []byte("gopher")))

	// U+212A KELVIN SIGN folds to ASCII k under simple folding
	kelvin := []byte("\u212a")
	fmt.Println("kelvin fold:", bytes.EqualFold(kelvin, []byte("k")))
	fmt.Println("exact equal:", bytes.Equal(kelvin, []byte("k")))

	// case-insensitive key lookup keeps the stored spelling intact
	headers := map[string]string{"Accept": "application/json"}
	for key, value := range headers {
		if bytes.EqualFold([]byte(key), []byte("accept")) {
			fmt.Println("header value:", value)
		}
	}
}

How the snippet works

Structurally, expect the function main and nothing else.

Iteration happens inside for key, value := range headers — everything outside it runs once.

What to watch for

  • By construction it needs nothing — no registry lookup, no lockfile entry.

Applying it

Look for the ascii fold:, length differs:, kelvin fold: prefixes when the file runs; they tie each line to its step.

Statements that matter most here

for key, value := range headers {
	if bytes.EqualFold([]byte(key), []byte("accept")) {
		fmt.Println("header value:", value)
	}
}

Interactive Tools for This Topic

FAQ

What does Bytes EqualFold ASCII Case print when run?
Invoke the file as-is — the `ascii fold:`, `length differs:`, `kelvin fold:` prefixes identify which step printed each line.