In Go, a non-point map is a map where the key and value types are not pointers. Here's an example of how you can marshal and unmarshal a non-point map to JSON:

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	// Create a non-point map
	data := map[string]int{
		"foo": 1,
		"bar": 2,
	}

	// Marshal the map to JSON
	jsonData, err := json.Marshal(data)
	if err != nil {
		fmt.Println("Error marshaling JSON:", err)
		return
	}

	fmt.Println("JSON data:", string(jsonData))

	// Unmarshal the JSON back to a map
	var decodedData map[string]int
	err = json.Unmarshal(jsonData, &decodedData)
	if err != nil {
		fmt.Println("Error unmarshaling JSON:", err)
		return
	}

	fmt.Println("Decoded map:", decodedData)
}

In this example, the data variable is a non-point map with string keys and integer values. It is then marshaled to JSON using json.Marshal(), and the resulting JSON data is printed.

The JSON data is then unmarshaled back to a map using json.Unmarshal(), and the decoded map is printed.

Note that if you have a map with pointer values, you can still marshal and unmarshal it to JSON in a similar way

golang json non-point map

原文地址: https://www.cveoy.top/t/topic/isUX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录