Skip to content

Instantly share code, notes, and snippets.

@byyam
Created August 12, 2019 10:25
Show Gist options
  • Save byyam/aac524622a122b46a48725aaa66d2119 to your computer and use it in GitHub Desktop.
Save byyam/aac524622a122b46a48725aaa66d2119 to your computer and use it in GitHub Desktop.
interface compare nil
package main
import (
"fmt"
"reflect"
)
type State struct{}
func testnil1(a, b interface{}) bool {
return a == b
}
func testnil2(a *State, b interface{}) bool {
return a == b
}
func testnil3(a interface{}) bool {
return a == nil
}
func testnil4(a *State) bool {
return a == nil
}
func testnil5(a interface{}) bool {
v := reflect.ValueOf(a)
return !v.IsValid() || v.IsNil()
}
func main() {
var a *State
fmt.Println(testnil1(a, nil))
fmt.Println(testnil2(a, nil))
fmt.Println(testnil3(a))
fmt.Println(testnil4(a))
fmt.Println(testnil5(a))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment