1
0
Fork 0

Update docs, compare to normal not-equals null check

This commit is contained in:
Gregory Eremin 2020-10-06 00:18:02 +02:00
parent 635255152a
commit 7fa581aba3
3 changed files with 64 additions and 4 deletions

18
LICENSE Normal file
View File

@ -0,0 +1,18 @@
Copyright 2020 Gregory Eremin
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -1,8 +1,35 @@
## Checking for `nil` in Go
# isnil
`nil` check that works for interfaces too. Example code and benchmarks.
Benchmarks:
```
BenchmarkIsNilBasic-8 200000000 9.47 ns/op
BenchmarkIsNilInterface-8 200000000 9.18 ns/op
BenchmarkIsNilNil-8 1000000000 2.03 ns/op
#
# x != nil
#
# nil pointer
BenchmarkEqNilBasic
BenchmarkEqNilBasic-8 1000000000 0.247 ns/op
# nil interface
BenchmarkEqNilInterface
BenchmarkEqNilInterface-8 1000000000 0.247 ns/op
#
# IsNil(x)
#
# nil pointer
BenchmarkIsNilBasic
BenchmarkIsNilBasic-8 274689241 4.37 ns/op
# (*struct{})(nil)
BenchmarkIsNilInterface
BenchmarkIsNilInterface-8 277561260 4.26 ns/op
# nil
BenchmarkIsNilNil
BenchmarkIsNilNil-8 803470719 1.48 ns/op
```

View File

@ -31,6 +31,21 @@ func TestIsNil(t *testing.T) {
}
}
func BenchmarkEqNilBasic(b *testing.B) {
var v *int
for i := 0; i < b.N; i++ {
_ = (v != nil)
}
}
func BenchmarkEqNilInterface(b *testing.B) {
var v interface{}
v = (*foo)(nil)
for i := 0; i < b.N; i++ {
_ = (v != nil)
}
}
func BenchmarkIsNilBasic(b *testing.B) {
var v *int
for i := 0; i < b.N; i++ {