Go · Stage 11 – Beyond the Basics · Lesson 64 of 64

Security-Conscious Go

Apply safe habits to input, files, dependencies, secrets, and network code.

Go code

if len(username) > 20 {
    return errors.New("username too long")
}
Line-by-line explanation
  1. Len counts bytes in this simple limit.
  2. The condition rejects excessive input.
  3. A returned error explains the failure.
Expected output
(no output)

The function returns early when the limit is exceeded.

Validate input, set network timeouts, limit response sizes, keep secrets outside source code, use least privilege, review dependencies, check every error, and avoid building shell commands from user text.