Use strings.TrimPrefix

ID

go.use_trim_prefix

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Best Practice

Language

Go

Tags

best-practice, code-style

Description

Reports the hand-rolled prefix-strip idiom if strings.HasPrefix(s, p) { s = s[len(p):] }. The standard library expresses exactly this as s = strings.TrimPrefix(s, p).

Rationale

strings.TrimPrefix removes a leading prefix when present and returns the string unchanged otherwise, so the explicit HasPrefix guard and the manual slice are both redundant. The manual version also risks an off-by-one slice or slicing the wrong variable; the library call is shorter and harder to get wrong.

// Bad
if strings.HasPrefix(s, p) {
    s = s[len(p):]
}

// Good
s = strings.TrimPrefix(s, p)

Remediation

Replace the if/slice block with s = strings.TrimPrefix(s, p).