SoFunction
Updated on 2025-03-03

Go language small problems encountered (recommended)

1. Description of problem phenomenon

use(), During deserialization, scientific notation method appears, the reference code is as follows:

jsonStr := `{"number":1234567}`
result := make(map[string]interface{})
err := ([]byte(jsonStr), &result)
if err != nil {
	(err)
}
(result)

// Output// map[number:1.234567e+06]

This problem is not necessarily revealed. It will only become a scientific notation method when the number of digits is greater than 6 digits.

2. Description of problem impact

When the data structure is unknown, usemap[string]interface{}When receiving the deserialization result, if the number of digits is greater than 6 digits, it will become a scientific notation method and the places used will be affected.

3. Causes of the problem

fromencoding/jsonYou can find the answer and take a look at this comment:

// To unmarshal JSON into an interface value,
// Unmarshal stores one of these in the interface value:
//
//	bool, for JSON booleans
//	float64, for JSON numbers
//	string, for JSON strings
//	[]interface{}, for JSON arrays
//	map[string]interface{}, for JSON objects
//	nil for JSON null

It's becauseJSONWhen a relatively large number exists, it will be parsed intofloat64Types may appear in the form of scientific notation.

4. Solution to the problem

Plan 1

For cast type conversion, the reference code is as follows:

jsonStr := `{"number":1234567}`
result := make(map[string]interface{})
err := ([]byte(jsonStr), &result)
if err != nil {
	(err)
}
(int(result["number"].(float64)))

// Output// 1234567

Plan 2

Try to avoid usinginterface,rightjsonString structure defines structures, quick methods are available for online tools:/json-to-go/

type Num struct {
	Number int `json:"number"`
}

jsonStr := `{"number":1234567}`
var result Num
err := ([]byte(jsonStr), &result)
if err != nil {
	(err)
}
(result)

// Output// {1234567}

Plan 3

useUseNumber()method.

jsonStr := `{"number":1234567}`
result := make(map[string]interface{})
d := (([]byte(jsonStr)))
()
err := (&result)
if err != nil {
	(err)
}
(result)

// Output// map[number:1234567]

Be sure to pay attention at this timeresult["number"]data type!

(("type: %v", (result["number"])))

// Output// type: 

You can see through the codeActually it's the string type:

// A Number represents a JSON number literal.
type Number string

If you convert other types, refer to the following code:

// Convert to int64numInt, _ := result["number"].().Int64()
(("value: %v, type: %v", numInt, (numInt)))

// Output// value: 1234567, type: int64

// Convert to stringnumStr := result["number"].().String()
(("value: %v, type: %v", numStr, (numStr)))

// Output// value: 1234567, type: string

This is the end of this article about the small problems encountered by Go language (recommended). For more related Go language content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!