Can't return nil, but zero value of slice
        Posted  
        
            by 
                Sergi
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Sergi
        
        
        
        Published on 2012-12-20T09:58:26Z
        Indexed on 
            2012/12/20
            11:03 UTC
        
        
        Read the original article
        Hit count: 248
        
go
I am having the case in which a function with the following code:
func halfMatch(text1, text2 string) []string {
    ...
    if (condition) {
         return nil // That's the final code path)
    }
    ...
}
is returning []string(nil) instead of nil. At first, I thought that perhaps returning nil in a function with a particular return type would just return an instance of a zero-value for that type. But then I tried a simple test and that is not the case.
Does anybody know why would nil return an empty string slice?
© Stack Overflow or respective owner