Created
August 12, 2019 10:25
-
-
Save byyam/aac524622a122b46a48725aaa66d2119 to your computer and use it in GitHub Desktop.
interface compare nil
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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