Slice zero-value behavior - nil slice is usable
Scanned 2/12/2026
npx -y skills add JamesPrial/claudefiles --skill slice --agent claude-codeInstalls into .claude/skills of the current project.
Are you the author of Slice?
Add the live security badge to your README — it updates automatically with every re-scan.
[](https://www.skillsdirectory.com/skills/jamesprial-slice)More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.
---
name: go-nil-slice
description: Slice zero-value behavior - nil slice is usable
---
# Slice Zero-Value
## Problem
Nil slices are valid and usable, but behavior differs from empty slices.
## Pattern
### CORRECT - Nil slice works with append
```go
var s []int // nil slice
s = append(s, 1, 2, 3) // OK, creates new backing array
fmt.Println(s) // [1 2 3]
```
### Nil vs Empty Slice
```go
var nilSlice []int // nil slice
emptySlice := []int{} // empty slice (non-nil)
len(nilSlice) == 0 // true
len(emptySlice) == 0 // true
nilSlice == nil // true
emptySlice == nil // false
json.Marshal(nilSlice) // "null"
json.Marshal(emptySlice) // "[]"
```
## When It Matters
```go
// JSON encoding differs
type Response struct {
Items []string // Will encode as null if nil, [] if empty
}
```
## Quick Fix
- [ ] Nil slices work with append, len, range
- [ ] Use nil for "no data", []T{} for "empty data"
- [ ] Check JSON encoding behavior if needed
Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.
No comments yet. Be the first to comment!