Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to solve the problem that the int64 type field returned by Go is beyond the range of javascript Number?

Shulou Source: shulou.com Published: 2022-05-31 11:47:24 11月16日 Update

This article Xiaobian introduces in detail for you "Go returns int64 type field how to solve the javascript Number range", the content is detailed, the steps are clear, and the details are handled properly. I hope this "Go return int64 type field how to solve the javascript Number" article can help you solve your doubts, following the editor's ideas slowly in depth, together to learn new knowledge.

Problem description

The json returned by the Go service is in the following format, where deviceId_O is: 144115188584281742

And the deviceId_O that the front end gets becomes: 144115188584281730. The last two digits changed, resulting in unsuccessful debugging no matter how much you debug.

In fact, this problem is not difficult to locate, you only need to check the difference between Preview and Response in the debugging tools to compare it, but it will be weird when you encounter this problem for the first time.

Javascript Number maximum

The Number object provided by Javascript provides a MAX_SAFE_INTEGER, which is a fixed value: 9007199254740991. So why is it not safe beyond this range? Look at the example:

Go Int64 maximum

We use this code to get the maximum value of int64, which is 9223372036854775807.

I64: = int64 (^ uint (0) > > 1) fmt.Println (i64)

Solution

This problem can be solved by returning a value as a string when the Go service returns json. For example, the deviceId in the first figure in this article can be returned.

After reading this, the article "how to solve the problem that Go returns int64 type field beyond the scope of javascript Number" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the article, you are welcome to follow the industry information channel.

Tags: Range field type maximum article maximum problem content service propriety weirdness security success code example front end character string object is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Huawei vpn MySQL Linux