如何处理我们在golang中发送和接收的数据

how can I decode the json object that has been sent in javascript to server and save them to variable op1,op2,opr.

and in java script i wanna decode the response sent by the server and save it variable result.

java script code:

var calculate = { 
                operand1 : null,
                operand2 : null,
                operator : null
};

function UserAction() {
    var xhttp = new XMLHttpRequest();
    xhttp.open("POST", "http://localhost:8000/", true);
    xhttp.setRequestHeader("Content-type", "application/json");
    xhttp.send(calculate);
    var response = (xhttp.responseText);
    console.log(response);
}
UserAction();

go server code:

package main
import ("fmt"
        "net/http"
        "encoding/json"
)


type answer struct {
    result float64
}


func index(w http.ResponseWriter, r *http.Request) {
    ans := answer{result: 30}
    fmt.Println(r)
    w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
    w.Header().Set("Content-Type", "application/json; charset=UTF-8")
    w.Header().Set("Access-Control-Allow-Origin", "*")
    w.WriteHeader(http.StatusOK)
    if err := json.NewEncoder(w).Encode(ans); err != nil {
        panic(err)
    }    
}

func main() {
    http.HandleFunc("/",index)
    http.ListenAndServe(":8000", nil)

}

The same way you can encode a payload in json in a http response, you can decode the body of a request.

err:= json.NewDecoder(request.Body).Decode(&answer);

Read Go documentation for future reference, and avoid any third party tutorial that doesn't clearly reference to the documentation.

https://golang.org/pkg/encoding/json/