gvalue

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2025 License: MIT Imports: 11 Imported by: 2

README

gvalue 包

简介

gvalue 包提供了通用值类型包装器,支持将任意类型的值转换为各种 Go 基本类型。

功能特性

  • 类型转换:支持转换为 int、string、bool、float 等类型
  • 安全转换:转换失败返回零值而不是 panic
  • 链式调用:支持链式操作
  • 空值处理:智能处理 nil 和空值

安装

go get github.com/snail007/gmc/util/value

快速开始

package main

import (
    "fmt"
    "github.com/snail007/gmc/util/value"
)

func main() {
    // 创建 Value
    v := gvalue.New(42)
    
    // 转换为不同类型
    fmt.Println(v.Int())     // 42
    fmt.Println(v.String())  // "42"
    fmt.Println(v.Float64()) // 42.0
    fmt.Println(v.Bool())    // true(非零值为 true)
    
    // 字符串值
    s := gvalue.New("123")
    fmt.Println(s.Int())     // 123
    fmt.Println(s.String())  // "123"
    
    // AnyValue 类型
    any := gvalue.NewAny("hello")
    fmt.Println(any.String())   // "hello"
    fmt.Println(any.IsEmpty())  // false
    
    // 处理 nil
    nilVal := gvalue.New(nil)
    fmt.Println(nilVal.String()) // ""
    fmt.Println(nilVal.Int())    // 0
}

API 参考

Value 类型
func New(v interface{}) *Value

方法:

  • String() string:转换为字符串
  • Int() int:转换为 int
  • Int64() int64:转换为 int64
  • Uint() uint:转换为 uint
  • Uint64() uint64:转换为 uint64
  • Float32() float32:转换为 float32
  • Float64() float64:转换为 float64
  • Bool() bool:转换为 bool
  • Interface() interface{}:获取原始值
  • Bytes() []byte:转换为字节数组
AnyValue 类型
func NewAny(v interface{}) *AnyValue

方法:

  • 包含 Value 的所有方法
  • IsEmpty() bool:是否为空
  • IsNil() bool:是否为 nil

类型转换规则

转 String
  • 数字:转换为字符串表示
  • 布尔:转换为 "true" 或 "false"
  • nil:返回空字符串
转 Int/Int64
  • 字符串:解析数字字符串
  • 布尔:true=1, false=0
  • 浮点:向下取整
  • nil:返回 0
转 Bool
  • 数字:非零为 true
  • 字符串:"true", "1", "yes", "on" 为 true
  • nil:返回 false
转 Float
  • 字符串:解析浮点数字符串
  • 布尔:true=1.0, false=0.0
  • 整数:转换为浮点数
  • nil:返回 0.0

使用场景

  1. 配置解析:处理配置文件中的值
  2. HTTP 参数:转换 URL 参数和表单数据
  3. 环境变量:处理环境变量值
  4. 数据库结果:转换数据库查询结果
  5. JSON 数据:处理动态 JSON 数据

示例

处理配置值
package main

import (
    "fmt"
    "github.com/snail007/gmc/util/value"
)

func main() {
    config := map[string]interface{}{
        "port":    "8080",
        "debug":   "true",
        "timeout": 30,
    }
    
    port := gvalue.New(config["port"]).Int()
    debug := gvalue.New(config["debug"]).Bool()
    timeout := gvalue.New(config["timeout"]).Int()
    
    fmt.Printf("Port: %d, Debug: %v, Timeout: %d\n", 
        port, debug, timeout)
}
安全的类型转换
package main

import (
    "fmt"
    "github.com/snail007/gmc/util/value"
)

func main() {
    // 即使值无法转换,也不会 panic
    v1 := gvalue.New("not a number")
    fmt.Println(v1.Int()) // 0
    
    v2 := gvalue.New(nil)
    fmt.Println(v2.String()) // ""
    
    v3 := gvalue.New("123abc")
    fmt.Println(v3.Int()) // 123(尽可能解析)
}

注意事项

  1. 零值返回:转换失败返回零值而不是错误
  2. 精度损失:浮点数转整数会丢失小数部分
  3. 字符串解析:字符串转数字会尽可能解析前缀数字
  4. 布尔转换:字符串转布尔不区分大小写

相关链接

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(sliceInterface interface{}, value interface{}) bool

Contains check the input slice if contains the value

func FormatByteSize

func FormatByteSize(v uint64) string

FormatByteSize convert byte size v to human-readable format, for example 1024 -> 1K

func FormatNumber

func FormatNumber(v interface{}) string

FormatNumber return split by comma format of number v. v must be intX, uintX or intX string, uintX string, example: 10000 -> 10,000

func IndexOf

func IndexOf(sliceInterface interface{}, value interface{}) int

IndexOf returns the index of value in slice, if not found -1 returned

func IsEmpty

func IsEmpty(object interface{}) bool

IsEmpty if object is IsNil with true, or is empty string, return true, others false.

func IsNil

func IsNil(object interface{}) bool

IsNil if object is nil or a nil pointer variable or nil builtin pointer variable(chan,func,interface,map,ptr,slice) return true, others false.

func MapToStruct

func MapToStruct(mapData map[string]interface{}, _value interface{}) (newStruct interface{}, err error)

MapToStruct same with MapToStructWithTag, but `json` is used as tag name by default

func MapToStructWithTag

func MapToStructWithTag(mapData map[string]interface{}, _value interface{}, tag string) (newStruct interface{}, err error)

MapToStructWithTag convert map to struct, _value is a pointer to strut, if key 'tag' not exists in the map, the filed name will used as map key. Returned newStruct is a struct, for example: newStruct.(User) to assert it type. Details refer to TestMapToStruct. Ignore filed, set tag tagName:"-", for example: User{ Age int `tagName:"-"` }

func ParseByteSize

func ParseByteSize(v string) uint64

ParseByteSize convert human-readable byte size format v to byte size, v can be: 1, 1B, 1KB, 1.1KB, 1MB, 1GB, 1TB, 1PB, 1EB

func ParseNumber

func ParseNumber(v string) int64

ParseNumber convert human-readable number format v to int64, example: 100,123 -> 100123

Types

type AnyValue

type AnyValue struct {
	// contains filtered or unexported fields
}

func Keys

func Keys(_map interface{}) *AnyValue

Keys input _map is a map, returns its keys as slice, AnyValue.xxxSlice to get the typed slice.

func MustAny

func MustAny(v interface{}, err error) (value *AnyValue)

MustAny ignore err, return *AnyValue of v

func NewAny

func NewAny(val interface{}) *AnyValue

NewAny wrap the val with type *AnyValue, *AnyValue.XXX() convert val to type XXX.

func (*AnyValue) Bool

func (s *AnyValue) Bool() bool

func (*AnyValue) BoolSlice

func (s *AnyValue) BoolSlice() []bool

func (*AnyValue) Bytes

func (s *AnyValue) Bytes() []byte

func (*AnyValue) Duration

func (s *AnyValue) Duration() time.Duration

func (*AnyValue) DurationSlice

func (s *AnyValue) DurationSlice() []time.Duration

func (*AnyValue) Float32

func (s *AnyValue) Float32() float32

func (*AnyValue) Float32Slice

func (s *AnyValue) Float32Slice() []float32

func (*AnyValue) Float64

func (s *AnyValue) Float64() float64

func (*AnyValue) Float64Slice

func (s *AnyValue) Float64Slice() []float64

func (*AnyValue) Int

func (s *AnyValue) Int() int

func (*AnyValue) Int8

func (s *AnyValue) Int8() int8

func (*AnyValue) Int8Slice

func (s *AnyValue) Int8Slice() []int8

func (*AnyValue) Int32

func (s *AnyValue) Int32() int32

func (*AnyValue) Int32Slice

func (s *AnyValue) Int32Slice() []int32

func (*AnyValue) Int64

func (s *AnyValue) Int64() int64

func (*AnyValue) Int64Slice

func (s *AnyValue) Int64Slice() []int64

func (*AnyValue) IntSlice

func (s *AnyValue) IntSlice() []int

func (*AnyValue) Map

func (s *AnyValue) Map() map[string]interface{}

func (*AnyValue) MapBool

func (s *AnyValue) MapBool() map[string]bool

func (*AnyValue) MapFloat32

func (s *AnyValue) MapFloat32() map[string]float32

func (*AnyValue) MapFloat64

func (s *AnyValue) MapFloat64() map[string]float64

func (*AnyValue) MapInt

func (s *AnyValue) MapInt() map[string]int

func (*AnyValue) MapInt8

func (s *AnyValue) MapInt8() map[string]int8

func (*AnyValue) MapInt32

func (s *AnyValue) MapInt32() map[string]int32

func (*AnyValue) MapInt64

func (s *AnyValue) MapInt64() map[string]int64

func (*AnyValue) MapString

func (s *AnyValue) MapString() map[string]string

func (*AnyValue) MapUint

func (s *AnyValue) MapUint() map[string]uint

func (*AnyValue) MapUint8

func (s *AnyValue) MapUint8() map[string]uint8

func (*AnyValue) MapUint32

func (s *AnyValue) MapUint32() map[string]uint32

func (*AnyValue) MapUint64

func (s *AnyValue) MapUint64() map[string]uint64

func (*AnyValue) Slice

func (s *AnyValue) Slice() []interface{}

func (*AnyValue) String

func (s *AnyValue) String() string

func (*AnyValue) StringSlice

func (s *AnyValue) StringSlice() []string

func (*AnyValue) Time

func (s *AnyValue) Time() time.Time

func (*AnyValue) Uint

func (s *AnyValue) Uint() uint

func (*AnyValue) Uint8

func (s *AnyValue) Uint8() uint8

func (*AnyValue) Uint8Slice

func (s *AnyValue) Uint8Slice() []uint8

func (*AnyValue) Uint32

func (s *AnyValue) Uint32() uint32

func (*AnyValue) Uint32Slice

func (s *AnyValue) Uint32Slice() []uint32

func (*AnyValue) Uint64

func (s *AnyValue) Uint64() uint64

func (*AnyValue) Uint64Slice

func (s *AnyValue) Uint64Slice() []uint64

func (*AnyValue) UintSlice

func (s *AnyValue) UintSlice() []uint

func (*AnyValue) Val

func (s *AnyValue) Val() interface{}

type Value

type Value struct {
	// contains filtered or unexported fields
}

func GetValueAt

func GetValueAt(sliceOrArray interface{}, idx int, defaultValue interface{}) *Value

GetValueAt sliceOrArray is slice or array, idx is the element to get, if idx invalid or sliceOrArray is not slice or array, the default value will be used

func Must

func Must(v interface{}, err error) (value *Value)

Must ignore err, return *Must of v

func New

func New(val interface{}) *Value

New wrap the val with type *Value, type val and *Value.XXX() must be same.

func (*Value) Bool

func (s *Value) Bool() bool

func (*Value) Bytes

func (s *Value) Bytes() []byte

func (*Value) Duration

func (s *Value) Duration() time.Duration

func (*Value) Float32

func (s *Value) Float32() float32

func (*Value) Float64

func (s *Value) Float64() float64

func (*Value) Int

func (s *Value) Int() int

func (*Value) Int8

func (s *Value) Int8() int8

func (*Value) Int32

func (s *Value) Int32() int32

func (*Value) Int64

func (s *Value) Int64() int64

func (*Value) Map

func (s *Value) Map() map[string]interface{}

func (*Value) MapBool

func (s *Value) MapBool() map[string]bool

func (*Value) MapFloat32

func (s *Value) MapFloat32() map[string]float32

func (*Value) MapFloat64

func (s *Value) MapFloat64() map[string]float64

func (*Value) MapInt

func (s *Value) MapInt() map[string]int

func (*Value) MapInt8

func (s *Value) MapInt8() map[string]int8

func (*Value) MapInt32

func (s *Value) MapInt32() map[string]int32

func (*Value) MapInt64

func (s *Value) MapInt64() map[string]int64

func (*Value) MapSlice

func (s *Value) MapSlice() map[string][]interface{}

func (*Value) MapString

func (s *Value) MapString() map[string]string

func (*Value) MapStringSlice

func (s *Value) MapStringSlice() map[string][]string

func (*Value) MapUint

func (s *Value) MapUint() map[string]uint

func (*Value) MapUint8

func (s *Value) MapUint8() map[string]uint8

func (*Value) MapUint32

func (s *Value) MapUint32() map[string]uint32

func (*Value) MapUint64

func (s *Value) MapUint64() map[string]uint64

func (*Value) Slice

func (s *Value) Slice() []interface{}

func (*Value) String

func (s *Value) String() string

func (*Value) StringSlice

func (s *Value) StringSlice() []string

func (*Value) Time

func (s *Value) Time() time.Time

func (*Value) Uint

func (s *Value) Uint() uint

func (*Value) Uint8

func (s *Value) Uint8() uint8

func (*Value) Uint32

func (s *Value) Uint32() uint32

func (*Value) Uint64

func (s *Value) Uint64() uint64

func (*Value) Val

func (s *Value) Val() interface{}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL