An open API service indexing awesome lists of open source software.

https://github.com/chen3feng/safecast

Safe Numeric Type Cast Library for Go
https://github.com/chen3feng/safecast

Last synced: 10 months ago
JSON representation

Safe Numeric Type Cast Library for Go

Awesome Lists containing this project

README

          

# safecast

Safe Numeric Type Cast Library for Go

English | [简体中文](README_zh.md)

[![License Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-red.svg)](COPYING)
[![Golang](https://img.shields.io/badge/Language-go1.18+-blue.svg)](https://go.dev/)
![Build Status](https://github.com/chen3feng/safecast/actions/workflows/go.yml/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/chen3feng/safecast/badge.svg?branch=master)](https://coveralls.io/github/chen3feng/safecast?branch=master)
[![GoReport](https://goreportcard.com/badge/github.com/securego/gosec)](https://goreportcard.com/report/github.com/chen3feng/safecast)
[![Go Reference](https://pkg.go.dev/badge/github.com/chen3feng/safecast.svg)](https://pkg.go.dev/github.com/chen3feng/safecast)

Safe numeric type cast library. suppoer all integral and floating types, except uintptr.

This library depends on go generics, which is introduced in 1.18+.

Usage:

```go
val, ok := To[type](value)
```

`ok == false` indicates overflow occured. but whatever,`val` is always equals to the result of the normal type cast (`type(value)`) expression。

There are also non-parametric forms of the target types:

````go
val, ok := ToInt32(value)
````

The usage is the same as the according full generic version, and the performance will be better.

# safecast

```go
import "github.com/chen3feng/safecast"
```

Package safecast provide a safe way to cast a numeric value from type A to type B, with overflow and underflow check.

## Index

- [func To[ToType numericType, FromType numericType](value FromType) (result ToType, ok bool)](<#func-to>)
- [func ToFloat32[FromType numericType](value FromType) (float32, bool)](<#func-tofloat32>)
- [func ToFloat64[FromType numericType](value FromType) (float64, bool)](<#func-tofloat64>)
- [func ToInt[FromType numericType](value FromType) (int, bool)](<#func-toint>)
- [func ToInt16[FromType numericType](value FromType) (int16, bool)](<#func-toint16>)
- [func ToInt32[FromType numericType](value FromType) (int32, bool)](<#func-toint32>)
- [func ToInt64[FromType numericType](value FromType) (int64, bool)](<#func-toint64>)
- [func ToInt8[FromType numericType](value FromType) (int8, bool)](<#func-toint8>)
- [func ToUint[FromType numericType](value FromType) (uint, bool)](<#func-touint>)
- [func ToUint16[FromType numericType](value FromType) (uint16, bool)](<#func-touint16>)
- [func ToUint32[FromType numericType](value FromType) (uint32, bool)](<#func-touint32>)
- [func ToUint64[FromType numericType](value FromType) (uint64, bool)](<#func-touint64>)
- [func ToUint8[FromType numericType](value FromType) (uint8, bool)](<#func-touint8>)

## func [To]()

```go
func To[ToType numericType, FromType numericType](value FromType) (result ToType, ok bool)
```

To converts a numeric value from the FromType to the specified ToType type safely. result will always be same as the usual type cast \(type\(value\)\), but ok is false when overflow or underflow occured.

Example (Float Overflow)

```go
package main

import (
"fmt"
"github.com/chen3feng/safecast"
"math"
)

func main() {
n, ok := safecast.To[float32](math.MaxFloat32 * 2)
fmt.Print(n, ok)
}
```

#### Output

```
+Inf false
```

Example (Int No Overflow)

```go
package main

import (
"fmt"
"github.com/chen3feng/safecast"
)

func main() {
b, ok := safecast.To[byte](255)
fmt.Print(b, ok)
}
```

#### Output

```
255 true
```

Example (Int Overflow)

```go
package main

import (
"fmt"
"github.com/chen3feng/safecast"
)

func main() {
b, ok := safecast.To[byte](256)
fmt.Print(b, ok)
}
```

#### Output

```
0 false
```

Example (Value In Range)

```go
package main

import (
"fmt"
"github.com/chen3feng/safecast"
)

func main() {
n, ok := safecast.To[uint](1)
fmt.Print(n, ok)
}
```

#### Output

```
1 true
```

Example (Value Out Of Range)

```go
package main

import (
"fmt"
"github.com/chen3feng/safecast"
)

func main() {
n, ok := safecast.To[uint32](-1)
fmt.Print(n, ok)
}
```

#### Output

```
4294967295 false
```

## func [ToFloat32]()

```go
func ToFloat32[FromType numericType](value FromType) (float32, bool)
```

ToFloat32 converts value to float32 type safely. result will always be same as the usual type cast\(float32\(value\)\), but ok is false when overflow or underflow occured.

## func [ToFloat64]()

```go
func ToFloat64[FromType numericType](value FromType) (float64, bool)
```

ToFloat64 converts value to float64 type safely. result will always be same as the usual type cast\(float64\(value\)\), but ok is false when overflow or underflow occured.

## func [ToInt]()

```go
func ToInt[FromType numericType](value FromType) (int, bool)
```

ToInt converts value to int type safely. result will always be same as the usual type cast\(int\(value\)\), but ok is false when overflow or underflow occured.

## func [ToInt16]()

```go
func ToInt16[FromType numericType](value FromType) (int16, bool)
```

ToInt16 converts value to int16 type safely. result will always be same as the usual type cast\(int16\(value\)\), but ok is false when overflow or underflow occured.

## func [ToInt32]()

```go
func ToInt32[FromType numericType](value FromType) (int32, bool)
```

ToInt32 converts value to int32 type safely. result will always be same as the usual type cast\(int32\(value\)\), but ok is false when overflow or underflow occured.

## func [ToInt64]()

```go
func ToInt64[FromType numericType](value FromType) (int64, bool)
```

ToInt64 converts value to int64 type safely. result will always be same as the usual type cast\(int64\(value\)\), but ok is false when overflow or underflow occured.

## func [ToInt8]()

```go
func ToInt8[FromType numericType](value FromType) (int8, bool)
```

ToInt8 converts value to int8 type safely. result will always be same as the usual type cast\(int8\(value\)\), but ok is false when overflow or underflow occured.

## func [ToUint]()

```go
func ToUint[FromType numericType](value FromType) (uint, bool)
```

ToUint converts value to uint type safely. result will always be same as the usual type cast\(uint\(value\)\), but ok is false when overflow or underflow occured.

## func [ToUint16]()

```go
func ToUint16[FromType numericType](value FromType) (uint16, bool)
```

ToUint16 converts value to uint16 type safely. result will always be same as the usual type cast\(uint16\(value\)\), but ok is false when overflow or underflow occured.

## func [ToUint32]()

```go
func ToUint32[FromType numericType](value FromType) (uint32, bool)
```

ToUint32 converts value to uint32 type safely. result will always be same as the usual type cast\(uint32\(value\)\), but ok is false when overflow or underflow occured.

## func [ToUint64]()

```go
func ToUint64[FromType numericType](value FromType) (uint64, bool)
```

ToUint64 converts value to uint64 type safely. result will always be same as the usual type cast\(uint64\(value\)\), but ok is false when overflow or underflow occured.

## func [ToUint8]()

```go
func ToUint8[FromType numericType](value FromType) (uint8, bool)
```

ToUint8 converts value to uint8 type safely. result will always be same as the usual type cast\(uint8\(value\)\), but ok is false when overflow or underflow occured.

Generated by [gomarkdoc]()