https://github.com/chyroc/reflectx
go reflect tool
https://github.com/chyroc/reflectx
go reflect
Last synced: 10 months ago
JSON representation
go reflect tool
- Host: GitHub
- URL: https://github.com/chyroc/reflectx
- Owner: chyroc
- License: apache-2.0
- Created: 2021-03-30T02:49:45.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-11-23T13:38:24.000Z (over 3 years ago)
- Last Synced: 2025-03-24T02:18:47.167Z (over 1 year ago)
- Topics: go, reflect
- Language: Go
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# reflectx
[](https://codecov.io/gh/chyroc/reflectx)
[](https://goreportcard.com/report/github.com/chyroc/reflectx)
[](https://github.com/chyroc/reflectx/actions)
[](https://opensource.org/licenses/Apache-2.0)
[](https://pkg.go.dev/github.com/chyroc/reflectx)
go reflect tool
## Install
```shell
go get github.com/chyroc/reflectx
```
## Usage
- check reflect.Value is some type
```go
// true
reflectx.IsUnsignedInt(reflect.ValueOf(uint(1)))
// false
reflectx.IsSignedInt(reflect.ValueOf(uint(1)))
// true
reflectx.IsInt(reflect.ValueOf(int(1)))
reflectx.IsInt(reflect.ValueOf(uint(1)))
// true
reflectx.IsBool(reflect.ValueOf(false))
```
- convert reflect.Value to some type
```go
// 1, nil
reflectx.ToInt64(reflect.ValueOf(uint(1)))
// 0, err
reflectx.ToInt64(reflect.ValueOf(false))
// true, nil
reflectx.ToBool(reflect.ValueOf(true))
// false, err
reflectx.ToBool(reflect.ValueOf(1))
// &true, nil
reflectx.ToPtr(reflect.ValueOf(true))
// &uint(1), err
reflectx.ToPtr(reflect.ValueOf(uint(1)))
```
- set value to reflect.Value
```go
var i int // -> 1
reflectx.SetInt(reflect.ValueOf(&i), 1)
var i uint // -> 2
reflectx.SetUint(reflect.ValueOf(&i), 2)
var i float6 // -> 1.1
reflectx.SetFloat(reflect.ValueOf(&i), 1.1)
```