Input sanitization makes incoming values uniform before service logic handles them. A translator service, for example, should treat capitalization and surrounding whitespace consistently rather than reject otherwise valid requests. section 3.3
A practical sanitization pass can:
- Convert text to lowercase. section 3.3
- Trim surrounding whitespace. section 3.3
- Reject unsupported words, languages, negative numbers, or invalid parameters after normalization. section 3.3
- Add tests for each variation and rerun them after the fix. section 3.3
Normalizing translator input
A Go service sanitizes both arguments before checking them: sanitizeInput applies strings.ToLower and strings.TrimSpace. Inputs such as "German", "Hello", and "hello " can then produce the same expected translation as their lowercase, trimmed forms. section 3.3
Listing 3.12 translator.go
package translation
import "strings"
func Translate(word string, language string) string {
word = sanitizeInput(word) #1
language = sanitizeInput(language) #2
if word != "hello" {
return ""
}
switch language {
case "english":
return "hello"
case "finnish":
return "hei"
case "german":
return "hallo"
default:
return ""
}
}
func sanitizeInput(w string) string { #3
w = strings.ToLower(w)
return strings.TrimSpace(w)
}
See the complete Go implementation, including sanitization before word validation and language selection. section 3.3


