JSON and Data Exchange
Struct Tags
Struct tags document the JSON field names used for exported Go fields.
Struct Tags
struct_tags.go
package main
import (
"fmt"
"strconv"
)
type Product struct {
SKU string `json:"sku"`
InStock bool `json:"in_stock"`
}
func main() {
var sku =
product := Product{SKU: sku, InStock: true}
text := fmt.Sprintf(`{"sku":%s,"in_stock":%t}`, strconv.Quote(product.SKU), product.InStock)
fmt.Println("sku=", product.SKU)
fmt.Println("inStock=", product.InStock)
fmt.Println("json=", text)
}
package main
import (
"fmt"
"strconv"
)
type Product struct {
SKU string `json:"sku"`
InStock bool `json:"in_stock"`
}
func main() {
var sku =
product := Product{SKU: sku, InStock: true}
text := fmt.Sprintf(`{"sku":%s,"in_stock":%t}`, strconv.Quote(product.SKU), product.InStock)
fmt.Println("sku=", product.SKU)
fmt.Println("inStock=", product.InStock)
fmt.Println("json=", text)
}
package main
import (
"fmt"
"strconv"
)
type Product struct {
SKU string `json:"sku"`
InStock bool `json:"in_stock"`
}
func main() {
var sku =
product := Product{SKU: sku, InStock: true}
text := fmt.Sprintf(`{"sku":%s,"in_stock":%t}`, strconv.Quote(product.SKU), product.InStock)
fmt.Println("sku=", product.SKU)
fmt.Println("inStock=", product.InStock)
fmt.Println("json=", text)
}
struct tags
JSON tags let a Go struct keep Go-style field names while publishing JSON-style field names.