There are two ways to initialize an empty slice in go:
var A = []string{}
var B []string
I would like to understand why these two are considered syntactically equivalent (or are they not?) when built-in libs like encoding/json
in methods like Marshall
output different values each respective case.
I might be missing some key idea on the compiler or the design choice but I would appreciate if someone could shed some light on this
Here’s a piece of code you can run on https://play.golang.org/:
package main
import (
"fmt"
"encoding/json"
)
func main() {
var A = []string{}
var B []string
msgA, errA := json.Marshal(A)
msgB, errB := json.Marshal(B)
fmt.Printf("%v; %v\n", string(msgA), errA)
fmt.Printf("%v; %v\n", string(msgB), errB)
}