in php I have this:
<?php
$x = array(1,2,3,4);
$y = json_encode($x);
echo $y;
?>
which its result is:
[1,2,3,4]
in Go when i use json.Marshal()
or jsonEncoder
encode method the result is:
[1 2 3 4]
which is not equivalent as the json_encode() result in php and I can't decode it in php.
Is there anyway to reach [1, 2, 3, 4] encoded result in go? (which has "," separator between each items)
When you use json.Marshal the result will be [1 2 3 4] but if you want to use your result as a string and send it to another place like redis , or print on the screen as a valid json, you should explicitly cast your result.
Here are the incorrect and correct codes:
package main
import (
"fmt"
"encoding/json"
)
func main() {
x := []int64{1, 2, 3, 4}
y, err := json.Marshal(x)
if err != nil {
panic(err)
}
fmt.Println(y)
}
and the correct code is : package main
import ( "fmt" "encoding/json" )
func main() {
x := []int64{1, 2, 3, 4}
y, err := json.Marshal(x)
if err != nil {
panic(err)
}
fmt.Println(string(y))
}