Skip to content

Commit

Permalink
docs: clarify Empty returns zero value
Browse files Browse the repository at this point in the history
https://go.dev/ref/spec#The_zero_value does not define "empty value". It informally mentions "empty value", meaning (non-nil && 0-length) slice or map.

> Note that the zero value for a slice or map type is not the same as an initialized but empty value of the same type.
  • Loading branch information
xuanhong committed Oct 18, 2024
1 parent 407b62d commit 3a1528b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion type_manipulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func FromAnySlice[T any](in []any) (out []T, ok bool) {
return result, true
}

// Empty returns an empty value.
// Empty returns the zero value (https://go.dev/ref/spec#The_zero_value).
func Empty[T any]() T {
var zero T
return zero
Expand Down
2 changes: 2 additions & 0 deletions type_manipulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ func TestEmpty(t *testing.T) {
is.Empty(Empty[int64]())
is.Empty(Empty[test]())
is.Empty(Empty[chan string]())
is.Nil(Empty[[]int]())
is.Nil(Empty[map[string]int]())
}

func TestIsEmpty(t *testing.T) {
Expand Down

0 comments on commit 3a1528b

Please sign in to comment.