Documentation
¶
Overview ¶
Example ¶
Example demonstrates basic usage of numx types
package main
import (
"encoding/json"
"fmt"
"github.com/gocrud/csgo/x/numx"
)
func main() {
type User struct {
ID numx.ID `json:"id"`
Score numx.BigInt `json:"score"`
Views numx.BigUint `json:"views"`
CreatedAt numx.Timestamp `json:"created_at"`
}
user := User{
ID: 9007199254740992, // Greater than JavaScript's MAX_SAFE_INTEGER
Score: -123456789012345,
Views: 18446744073709551,
CreatedAt: numx.Now(),
}
// Marshal to JSON - numbers become strings
data, _ := json.Marshal(user)
fmt.Printf("JSON: %s\n", data)
// Unmarshal from JSON - accepts both string and number
jsonStr := `{"id":"123","score":-456,"views":"789","created_at":"1703234567890"}`
var u User
json.Unmarshal([]byte(jsonStr), &u)
fmt.Printf("User ID: %d, Score: %d\n", u.ID.Int64(), u.Score.Int64())
}
Output:
Index ¶
- type BigInt
- type BigUint
- type ID
- type Timestamp
- func (t Timestamp) Int64() int64
- func (t Timestamp) MarshalJSON() ([]byte, error)
- func (t *Timestamp) Scan(value interface{}) error
- func (t Timestamp) String() string
- func (t Timestamp) Time() time.Time
- func (t *Timestamp) UnmarshalJSON(data []byte) error
- func (t Timestamp) Value() (driver.Value, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BigInt ¶
type BigInt int64
BigInt 是一个 int64 类型,在 JSON 序列化/反序列化时作为字符串处理,以避免在 JavaScript 中丢失精度。
func (BigInt) MarshalJSON ¶
MarshalJSON 实现 json.Marshaler 接口。
func (*BigInt) UnmarshalJSON ¶
UnmarshalJSON 实现 json.Unmarshaler 接口。
type BigUint ¶
type BigUint uint64
BigUint 是一个 uint64 类型,在 JSON 序列化/反序列化时作为字符串处理,以避免在 JavaScript 中丢失精度。
func (BigUint) MarshalJSON ¶
MarshalJSON 实现 json.Marshaler 接口。
func (*BigUint) UnmarshalJSON ¶
UnmarshalJSON 实现 json.Unmarshaler 接口。
type ID ¶
type ID int64
ID is an int64 type that serializes to/from JSON as a string to avoid precision loss in JavaScript.
Example ¶
ExampleID demonstrates ID type usage
package main
import (
"encoding/json"
"fmt"
"github.com/gocrud/csgo/x/numx"
)
func main() {
id := numx.ID(9007199254740992)
// JSON serialization
data, _ := json.Marshal(id)
fmt.Printf("JSON: %s\n", data)
// Type conversion
fmt.Printf("Int64: %d\n", id.Int64())
fmt.Printf("String: %s\n", id.String())
fmt.Printf("IsZero: %v\n", id.IsZero())
}
Output: JSON: "9007199254740992" Int64: 9007199254740992 String: 9007199254740992 IsZero: false
func (ID) MarshalJSON ¶
MarshalJSON implements json.Marshaler interface. Converts int64 to JSON string to prevent precision loss in JavaScript.
func (*ID) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler interface. Accepts both JSON string and number formats.
type Timestamp ¶
type Timestamp int64
Timestamp is an int64 Unix timestamp (milliseconds) that serializes to/from JSON as a string.
Example ¶
ExampleTimestamp demonstrates Timestamp type usage
package main
import (
"encoding/json"
"fmt"
"github.com/gocrud/csgo/x/numx"
)
func main() {
// Create timestamp
now := numx.Now()
// Convert to time.Time
t := now.Time()
fmt.Printf("Time: %v\n", t.Format("2006-01-02"))
// JSON serialization
data, _ := json.Marshal(now)
fmt.Printf("JSON: %s\n", data)
}
Output:
func (Timestamp) MarshalJSON ¶
MarshalJSON implements json.Marshaler interface.
func (*Timestamp) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler interface.