Skip to content

json unmarshal "null" to empty slice variable(etc: []string{} ) doesn't have err. But when marshal the variable, the value will be "null" instead of "[]" #26866

Closed
@Tron2016

Description

@Tron2016

What version of Go are you using (go version)?

go version go1.10.2

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

linux/amd64

What did you do?

ls := []string{}
if err := json.Unmarshal([]byte("null"), &ls); err != nil {
	fmt.Println(err)
}
fmt.Println(ls)
if result, err := json.Marshal(ls); err != nil {
	fmt.Println(err)
} else {
	fmt.Println(string(result))
}

What did you expect to see?

output:
[]
[]

What did you see instead?

output
[]
null

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions