It easy to get an empty list when working with string by using []string{}:
import (
"encoding/json"
"fmt"
)
func main() {
slice1 := []string{} // non-nil but zero-length
json1, _ := json.Marshal(slice1)
fmt.Printf("%s\n", json1) // []
}
The output of code above is [], BUT when I work with []byte even using []byte{} returns "". How should I get an empty list like what I get in []string{}?
import (
"encoding/json"
"fmt"
)
func main() {
slice2 := []byte{}
json2, _ := json.Marshal(slice2)
fmt.Printf("%s\n", json2) // ""
}