x_null

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 5 Imported by: 0

README

Null Types

x_null.Float64
x_null.Int64
x_null.String
x_null.Time

解决了什么问题?

  1. 数据库在int,string等类型时,同时可能允许null值,但是go中int,string等类型不允许null值,数据类型不一致就会报错。
  2. 前端传递null、空,基础类型无法区分。
  3. 前端传递字符串数字如:"123",基础类型int64等无法自动转换接收。

自定义类型介绍

type Int64 struct {
	Val   *int64
	Exist bool
}
  1. 前端传递空,Val=nil、Exist=false。
  2. 前端传递null,Val=nil、Exist=true。
  3. 前端传递字符串数字如:"123",Val=123、Exist=true。

自定义类型方法

暂时不要使用没有列出的方法。

func (i *Int64) Scan(value any) error

func (i Int64) Value() (driver.Value, error)

func (i Int64) MarshalJSON() ([]byte, error)

func (i *Int64) UnmarshalJSON(data []byte) error
func (i *Int64) UnmarshalText(text []byte) error
func (i *Int64) UnmarshalParam(param string) error


func (i Int64) String() string


// ValueOr 获取值,不存在则返回默认值
func (i *Int64) ValueOr(v int64) int64
// ValueOrZero 获取值,不存在则返回零值
func (i *Int64) ValueOrZero() int64
// IsZero go to json时omitempty标签是否忽略该字段
func (i Int64) IsZero() bool 
// IsExists 是否存在
func (i Int64) IsExists() bool
// IsExistsAndNotNull 存在且不为null
func (i Int64) IsExistsAndNotNull() bool
// IsExistsAndNull 存在且为null
func (i Int64) IsExistsAndNull() bool

Documentation

Index

Constants

View Source
const (
	// DateFormat 日期格式
	DateFormat = "2006-01-02"
	// TimeFormat 时间格式
	TimeFormat = "2006-01-02 15:04:05"
)

Variables

This section is empty.

Functions

func DecodeFloat64

func DecodeFloat64(value any) (any, error)

DecodeFloat 解码浮点数值

func DecodeInt64

func DecodeInt64(value any) (any, error)

DecodeInt 解码整数值

func DecodeString

func DecodeString(value any) (any, error)

DecodeString 解码字符串值

func DecodeTime added in v0.0.2

func DecodeTime(value any) (any, error)

DecodeTime 解码字符串

func ToFloat64

func ToFloat64(value any) (*float64, error)

ToFloat64 将任意值转换为 float64

func ToInt64

func ToInt64(value any) (*int64, error)

ToInt64 将任意值转换为 int64

func ToString

func ToString(value any) *string

ToString 将任意值转换为 string

func ToTime added in v0.0.2

func ToTime(value any) (*time.Time, error)

ToTime

Types

type Float64

type Float64 struct {
	Val   *float64
	Exist bool
}

Float64 支持前端传递null,int,float,string类型和不传值 前端传1,"1"都可以,都转换为float64类型: Float64{Val: 1.0, Exist: true} 前端null值: Float64{Val: nil, Exist: true} 前端""值: Float64{Val: nil, Exist: true} 前端没传值: Float64{Val: nil, Exist: false}

func NewFloat64

func NewFloat64(val float64) Float64

NewFloat64 创建一个带值的 Float64

func (*Float64) GetValue

func (i *Float64) GetValue() *float64

GetValue 获取值指针

func (Float64) IsExists

func (i Float64) IsExists() bool

IsExists 是否存在

func (Float64) IsExistsAndNotNull

func (i Float64) IsExistsAndNotNull() bool

IsExistsAndNotNull 存在且不为null

func (Float64) IsExistsAndNull

func (i Float64) IsExistsAndNull() bool

IsExistsAndNull 存在且为null

func (Float64) IsZero

func (i Float64) IsZero() bool

IsZero go to json时omitempty标签是否忽略该字段

func (Float64) MarshalJSON

func (f Float64) MarshalJSON() ([]byte, error)

MarshalJSON 实现json序列化接口

func (*Float64) Scan

func (f *Float64) Scan(value any) error

Scan gorm实现Scanner

func (*Float64) SetNull

func (i *Float64) SetNull() *Float64

SetNull 设置为null

func (*Float64) SetValue

func (i *Float64) SetValue(value float64) *Float64

SetValue 设置值

func (Float64) String

func (f Float64) String() string

String 实现fmt.Stringer接口

func (*Float64) UnmarshalJSON

func (i *Float64) UnmarshalJSON(data []byte) error

UnmarshalJSON 实现json反序列化接口

func (*Float64) UnmarshalParam

func (i *Float64) UnmarshalParam(param string) error

UnmarshalParam 实现gin框架的参数绑定接口

func (*Float64) UnmarshalText

func (i *Float64) UnmarshalText(text []byte) error

UnmarshalText 实现 encoding.TextUnmarshaler 接口

func (Float64) Value

func (f Float64) Value() (driver.Value, error)

Value gorm实现 Valuer

func (*Float64) ValueOr

func (i *Float64) ValueOr(v float64) float64

ValueOr 获取值,不存在则返回默认值

func (*Float64) ValueOrZero

func (i *Float64) ValueOrZero() float64

ValueOrZero 获取值,不存在则返回零值

type Int64

type Int64 struct {
	Val   *int64
	Exist bool
}

Int64 支持前端传递null,int,string类型和不传值 前端传1,"1"都可以,都转换为int64类型: Int64{Val: 1, Exist: true} 前端null值: Int64{Val: nil, Exist: true} 前端""值: Int64{Val: nil, Exist: true} 前端没传值: Int64{Val: nil, Exist: false}:结构体字段都是零值,并且Value接口返回nil,会忽略更新

func NewInt64

func NewInt64(val int64) Int64

NewInt64 创建一个带值的 Int64

func (*Int64) GetValue

func (i *Int64) GetValue() *int64

GetValue 获取值指针

func (Int64) IsExists

func (i Int64) IsExists() bool

IsExists 是否存在

func (Int64) IsExistsAndNotNull

func (i Int64) IsExistsAndNotNull() bool

IsExistsAndNotNull 存在且不为null

func (Int64) IsExistsAndNull

func (i Int64) IsExistsAndNull() bool

IsExistsAndNull 存在且为null

func (Int64) IsZero

func (i Int64) IsZero() bool

IsZero go to json时omitempty标签是否忽略该字段

func (Int64) MarshalJSON

func (i Int64) MarshalJSON() ([]byte, error)

MarshalJSON 实现json序列化接口

func (*Int64) Scan

func (i *Int64) Scan(value any) error

Scan gorm实现Scanner

func (*Int64) SetNull

func (i *Int64) SetNull()

SetNull 设置为null

func (*Int64) SetValue

func (i *Int64) SetValue(value int64)

SetValue 设置值

func (Int64) String

func (i Int64) String() string

String 实现fmt.Stringer接口

func (*Int64) UnmarshalJSON

func (i *Int64) UnmarshalJSON(data []byte) error

UnmarshalJSON 实现json反序列化接口,支持 int64, string,null类型,对于float64类型,判断转换前后是否相等,防止精度丢失

func (*Int64) UnmarshalParam

func (i *Int64) UnmarshalParam(param string) error

UnmarshalParam 实现gin框架的参数绑定接口

func (*Int64) UnmarshalText

func (i *Int64) UnmarshalText(text []byte) error

UnmarshalText 实现 encoding.TextUnmarshaler 接口

func (Int64) Value

func (i Int64) Value() (driver.Value, error)

Value gorm实现 Valuer

func (*Int64) ValueOr

func (i *Int64) ValueOr(v int64) int64

ValueOr 获取值,不存在则返回默认值

func (*Int64) ValueOrZero

func (i *Int64) ValueOrZero() int64

ValueOrZero 获取值,不存在则返回零值

type String

type String struct {
	Val   *string
	Exist bool
}

String 支持前端传递null,int,string类型和不传值 前端传1,"1"都可以,都转换为string类型: String{Val: "1", Exist: true} 前端null值: String{Val: nil, Exist: true} 前端""值: String{Val: "", Exist: true} 前端没传值: String{Val: nil, Exist: false}

func NewString

func NewString(val string) String

NewString 创建一个带值的 String

func (*String) GetValue

func (i *String) GetValue() *string

GetValue 获取值指针

func (String) IsExists

func (i String) IsExists() bool

IsExists 是否存在

func (String) IsExistsAndNotNull

func (i String) IsExistsAndNotNull() bool

IsExistsAndNotNull 存在且不为null

func (String) IsExistsAndNull

func (i String) IsExistsAndNull() bool

IsExistsAndNull 存在且为null

func (String) IsZero

func (i String) IsZero() bool

IsZero go to json时omitempty标签是否忽略该字段

func (String) MarshalJSON

func (i String) MarshalJSON() ([]byte, error)

MarshalJSON 实现json序列化接口

func (*String) Scan

func (i *String) Scan(value any) error

Scan gorm实现Scanner,支持string, nil类型

func (*String) SetNull

func (i *String) SetNull()

SetNull 设置为null

func (*String) SetValue

func (i *String) SetValue(value string)

SetValue 设置值

func (String) String

func (i String) String() string

String 实现fmt.Stringer接口

func (*String) UnmarshalJSON

func (i *String) UnmarshalJSON(data []byte) error

UnmarshalJSON 实现json反序列化接口

func (*String) UnmarshalParam

func (i *String) UnmarshalParam(param string) error

UnmarshalParam 实现gin框架的参数绑定接口

func (*String) UnmarshalText

func (i *String) UnmarshalText(text []byte) error

UnmarshalText 实现 encoding.TextUnmarshaler 接口

func (String) Value

func (i String) Value() (driver.Value, error)

Value gorm实现 Valuer

func (*String) ValueOr

func (i *String) ValueOr(v string) string

ValueOr 获取值,不存在则返回默认值

func (*String) ValueOrZero

func (i *String) ValueOrZero() string

ValueOrZero 获取值,不存在则返回零值

type Time

type Time struct {
	Val   *time.Time
	Exist bool
}

Time 自定义时间格式

func NewTime

func NewTime(val time.Time) Time

NewTime 创建一个带值的 Time

func (*Time) GetValue

func (i *Time) GetValue() *time.Time

GetValue 获取值指针

func (Time) IsExists

func (i Time) IsExists() bool

IsExists 是否存在

func (Time) IsExistsAndNotNull

func (i Time) IsExistsAndNotNull() bool

IsExistsAndNotNull 存在且不为null

func (Time) IsExistsAndNull

func (i Time) IsExistsAndNull() bool

IsExistsAndNull 存在且为null

func (Time) IsZero

func (i Time) IsZero() bool

IsZero go to json时omitempty标签是否忽略该字段

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

MarshalJSON 将Time类型的时间转化为JSON字符串格式

func (*Time) Scan

func (t *Time) Scan(value any) error

Scan 读取数据gorm调用

func (*Time) SetNull

func (i *Time) SetNull()

SetNull 设置为null

func (*Time) SetValue

func (i *Time) SetValue(value time.Time)

SetValue 设置值

func (Time) String

func (t Time) String() string

String 实现fmt.Stringer接口

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(bs []byte) error

UnmarshalJSON 实现json反序列化接口

func (*Time) UnmarshalParam

func (i *Time) UnmarshalParam(param string) error

UnmarshalParam 实现gin框架的参数绑定接口

func (*Time) UnmarshalText

func (i *Time) UnmarshalText(text []byte) error

UnmarshalText 实现 encoding.TextUnmarshaler 接口

func (Time) Value

func (t Time) Value() (driver.Value, error)

Value 写入数据库gorm调用

func (*Time) ValueOr

func (i *Time) ValueOr(v time.Time) time.Time

ValueOr 获取值,不存在则返回默认值

func (*Time) ValueOrZero

func (i *Time) ValueOrZero() time.Time

ValueOrZero 获取值,不存在则返回零值

Jump to

Keyboard shortcuts

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