Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt: Empty slice and nil slice are printed both as empty slice #12780

Closed
krnowak opened this issue Sep 29, 2015 · 3 comments
Closed

fmt: Empty slice and nil slice are printed both as empty slice #12780

krnowak opened this issue Sep 29, 2015 · 3 comments

Comments

@krnowak
Copy link

krnowak commented Sep 29, 2015

Please see following snippet (also saved in http://play.golang.org/p/bTc5CWT--P):

package main

import "fmt"
import "reflect"

func main() {
    var nilArray []string = nil
    emptyArray := []string{}
    if !reflect.DeepEqual(nilArray, emptyArray) {
        fmt.Printf("They are different as expected, let's see if this is going to be helpful message, like you would like to receive in some test\n")
        fmt.Printf("Expected:\n%+v\nGot:\n%+v\n", nilArray, emptyArray)
    }
}

I would expect it to print something like:

Expected:
<nil>
Got:
[]

but instead, I'm getting:

Expected:
[]
Got:
[]

Tracking what actually is different might be a PITA in some more complex data structures.

@cznic
Copy link
Contributor

cznic commented Sep 29, 2015

When the dictinction matters, the # verb modifier can be used to show it: http://play.golang.org/p/smJjDhLM-H

@krnowak
Copy link
Author

krnowak commented Sep 29, 2015

@cznic: Thanks for the hint. But the point probably still stands.

@ianlancetaylor ianlancetaylor changed the title fmt: Empty array and nil array are printed both as empty array. fmt: Empty slice and nil slice are printed both as empty slice Sep 29, 2015
@ianlancetaylor
Copy link
Contributor

This is intended behaviour and even documented behaviour.

@golang golang locked and limited conversation to collaborators Sep 28, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants