LoginSignup
0
0

More than 1 year has passed since last update.

go: json.Unmarshal(data, slice)

Last updated at Posted at 2022-05-10
package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	cases := []struct {
		in []byte
	}{
		{
			in: []byte(`[]`),
		},
		{
			in: []byte(`null`),
		},
		{
			in: []byte(``),
		},
		{
			in: []byte(nil),
		},
	}
	for _, tt := range cases {
		out := []string{}
		err := json.Unmarshal(tt.in, &out)
		if err != nil {
			fmt.Printf("FAIL: %#v, err: %v\n", []byte(tt.in), err)
		} else {
			fmt.Printf("PASS: %s, out: %#v\n", []byte(tt.in), out)
		}
	}
}
PASS: [], out: []string{}
PASS: null, out: []string(nil)
FAIL: []byte{}, err: unexpected end of JSON input
FAIL: []byte(nil), err: unexpected end of JSON input
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0