Skip to content
Go: Remove one exact prefix and verify it was present

Go: Remove one exact prefix and verify it was present

November 14, 2026

A line beginning with id: has a specific marker, not a set of removable characters. strings.CutPrefix checks that exact prefix and returns both the remainder and whether the marker was found.

Set up

Tested on Linux amd64 with Go 1.26.10. Use Bash with Go on your PATH, a text editor, and the mktemp, rm, and rmdir utilities. The example uses only the standard library. Keep the same terminal through cleanup, and stop if setup fails before saving the source.

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save as go-strings-cutprefix-exact-marker.go:

package main

import (
	"fmt"
	"strings"
)

func readID(line string) (string, error) {
	value, found := strings.CutPrefix(line, "id:")
	if !found {
		return "", fmt.Errorf("missing id prefix")
	}
	if value == "" {
		return "", fmt.Errorf("empty id")
	}
	return value, nil
}

func main() {
	for _, line := range []string{"id:42", "id:", "di:42", "ID:42", "id:id:42"} {
		value, err := readID(line)
		fmt.Printf("%q: value=%q error=%v\n", line, value, err)
	}
	if value, err := readID("id:42"); value != "42" || err != nil {
		panic("valid id")
	}
	if _, err := readID("di:42"); err == nil {
		panic("reordered prefix")
	}
	if value, err := readID("id:id:42"); value != "id:42" || err != nil {
		panic("single removal")
	}
	fmt.Printf("TrimLeft comparison: %q\n", strings.TrimLeft("di:42", "id:"))
}

Run:

go run go-strings-cutprefix-exact-marker.go

Observed output:

"id:42": value="42" error=<nil>
"id:": value="" error=empty id
"di:42": value="" error=missing id prefix
"ID:42": value="" error=missing id prefix
"id:id:42": value="id:42" error=<nil>
TrimLeft comparison: "42"

Read the result

Matching is case-sensitive and occurs only at the start. The prefix is removed once, so a second copy remains part of the value. The empty-value check belongs to the application; a present prefix does not imply that data follows it.

The comparison call uses TrimLeft with a cutset. It removes any leading characters belonging to that set, including the reordered marker. That is useful for trimming character classes but does not verify this line format.

Apply it carefully

The helper validates only the marker and a nonempty remainder. It does not require a numeric identifier, trim whitespace or reject additional colons. Add those rules if the format needs them. An empty prefix would always match, so keep a configurable marker nonempty when presence matters. This operation does no Unicode normalization or case folding. The returned string may share storage with the original, which can matter when retaining a small value from a very large input.

See the Go standard-library reference.

Clean up

Remove the saved source and the empty scratch directory. Go manages its build cache separately. If the directory is not empty, inspect its contents before removing anything else.

rm -- go-strings-cutprefix-exact-marker.go
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

Acknowledgements

This post was created with assistance from AI.

Last updated on