Double curly brackets in struct when using in Decode(&struct{}{})

I have this function in some code. What are the double curly brackets in struct that help to decide that it's not two JSONs? How does it work?

func readJSON(w http.ResponseWriter,r *http.Request,data interface{}) error {
    maxBytes := 1024 * 1024 
    r.Body = http.MaxBytesReader(w,r.Body,int64(maxBytes))
    dec := json.NewDecoder(r.Body)
    err := dec.Decode(data)

    if err != nil {
        return err

    err = dec.Decode(&struct{}{})
    if err != io.EOF {
        return errors.New("Body must Only contain 1 json ")
    return nil


  • Let's break it down:

    • struct{} is a type: a struct with no fields.
    • struct{}{} is a literal value: a new instance of the above type.
    • &struct{}{} is a pointer to the above literal value.

    By attempting to decode JSON a second time, it confirms that the body does not have a second JSON document after the first, for example:

      "foo": "bar"
      "foo": "qux"