Created
August 9, 2019 13:49
-
-
Save addlistener/556c8a5a5b3864f7aba0a6a0bb0e1063 to your computer and use it in GitHub Desktop.
json serialize nil slice
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
// https://forum.golangbridge.org/t/slice-pass-as-value-or-pointer/2866/2 | |
package main | |
import ( | |
"fmt" | |
"reflect" | |
"encoding/json" | |
) | |
type TTT struct { | |
arr []int | |
} | |
func main() { | |
fmt.Println("Hello, playground") | |
var a []int | |
b := []int{} | |
fmt.Printf("%T %v %v %v\n%T %v %v %v\n", a, a, reflect.TypeOf(a).String(), &a, b, b, reflect.TypeOf(b).String(), &b) | |
sA := TTT{arr: a} | |
sB := TTT{arr: b} | |
fmt.Println(sA, sB) | |
byteA, _ := json.Marshal(a) | |
byteB, _ := json.Marshal(b) | |
fmt.Println(string(byteA), string(byteB)) | |
byteSA, _ := json.Marshal(sA) | |
byteSB, _ := json.Marshal(sB) | |
fmt.Println(string(byteSA), string(byteSB)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment