Why is Go json.Marshal rejecting these struct tags? What is proper syntax for json tags? [duplicate]

Oh my goodness! I just figured it out. There is no space allowed between json: and the field name "name".

The “go vet” error message ("bad syntax") is remarkably unhelpful.

The following code works. Can you see the difference?

package main

import (
    "encoding/json"
    "fmt"
)

type Person struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

func main() {
    p := Person{Name: "Alice", Age: 29}
    bytes, _ := json.Marshal(p)
    fmt.Println("JSON = ", string(bytes))
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)