go 語言中的nil

In Go, nil is the zero value for pointers, interfaces, maps, slices, channels and function types, representing an uninitialized value.

nil doesn't mean some "undefined" state, it's a proper value in itself. An object in Go is nil simply if and only if it's value is nil, which it can only be if it's of one of the aforementioned types.

An error is an interface, so nil is a valid value for one, unlike for a string. For obvious reasons a nil error represents no error.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章