Ir al contenido

First Or Default Null (ARCH5078)

Esta página aún no está disponible en tu idioma.

Key go.performance.first-or-default-null · Severity warning · Category performance

Accessing [0] of a filtered slice without a length check panics when the result is empty. Add an emptiness guard before the access.

func firstAdmin(users []User) User {
admins := filter(users, isAdmin)
return admins[0]
}
func firstAdmin(users []User) (User, bool) {
admins := filter(users, isAdmin)
if len(admins) == 0 {
return User{}, false
}
return admins[0], true
}

Add a len(...) == 0 check before accessing the first element.