xtype

package module
v0.0.0-...-ec495aa Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2021 License: Apache-2.0 Imports: 1 Imported by: 1

README

goclub/type

go get github.com/goclub/type

go 的类型系统没有 int? 这样的语法来表达一些基础字段是 nil 还是zero value.有些业务场景需要实现类似 int? 来解决问题.

goclub/type 提供了 Option 类型来解决这类问题.

package main

import (
	xtype "github.com/goclub/type"
	"log"
)

// 利用 xtype.OptionInt 判断是否设置 age
func SetData(name string, age xtype.OptionInt) {
	if age.Valid() {
		log.Print("set age", age.Unwrap())
	} else {
		log.Print("not set age")
	}
}

func main() {
	SetData("goclub", xtype.Int(18)) // set age 18
	SetData("goclub", xtype.OptionInt{}) // not set age
}

xtype.OptionInt源码:

package xtype
type OptionInt struct {
	int int
	valid bool
}
func (o OptionInt) Valid() bool {
	return o.valid
}
func (o OptionInt) Unwrap() int {
	if o.valid {
		return o.int
	}
	return 0
}
func Int(i int) OptionInt {
	return OptionInt{ i, true}
}

其他基础类型也都实现了 Option 方法,通过 xtype.类型() 获取有效值, xtype.Option类型{} 表示无效值.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OptionBool

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

func Bool

func Bool(b bool) OptionBool

func (OptionBool) Unwrap

func (o OptionBool) Unwrap() bool

func (OptionBool) Valid

func (o OptionBool) Valid() bool

type OptionBytes

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

func Bytes

func Bytes(bytes []byte) OptionBytes

func (OptionBytes) Unwrap

func (o OptionBytes) Unwrap() []byte

func (OptionBytes) Valid

func (o OptionBytes) Valid() bool

type OptionComplex128

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

func Complex128

func Complex128(c complex128) OptionComplex128

func (OptionComplex128) Unwrap

func (o OptionComplex128) Unwrap() complex128

func (OptionComplex128) Valid

func (o OptionComplex128) Valid() bool

type OptionComplex64

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

func Complex64

func Complex64(c complex64) OptionComplex64

func (OptionComplex64) Unwrap

func (o OptionComplex64) Unwrap() complex64

func (OptionComplex64) Valid

func (o OptionComplex64) Valid() bool

type OptionDuration

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

func Duration

func Duration(d time.Duration) OptionDuration

func (OptionDuration) Unwrap

func (o OptionDuration) Unwrap() time.Duration

func (OptionDuration) Valid

func (o OptionDuration) Valid() bool

type OptionFloat32

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

func Float32

func Float32(f float32) OptionFloat32

func (OptionFloat32) Unwrap

func (o OptionFloat32) Unwrap() float32

func (OptionFloat32) Valid

func (o OptionFloat32) Valid() bool

type OptionFloat64

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

func Float64

func Float64(f float64) OptionFloat64

func (OptionFloat64) Unwrap

func (o OptionFloat64) Unwrap() float64

func (OptionFloat64) Valid

func (o OptionFloat64) Valid() bool

type OptionInt

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

func Int

func Int(i int) OptionInt
Example
package main

import (
	xtype "github.com/goclub/type"
	"log"
)

// 必须设置 age
func SetDataV1(name string, age int) {
	log.Print("set age", age)
}

// 可通过 notSetAge 控制不设置 age
func SetDataV2(name string, age int, notSetAge bool) {
	if notSetAge {
		log.Print("not set age")
	} else {
		log.Print("set age", age)
	}
}

// 传递nil则表示不设置 age
func SetDataV3(name string, age *int) {
	if age == nil {
		log.Print("not set age")
	} else {
		log.Print("set age", *age)
	}
}

// 利用 xtype.OptionInt 判断是否设置 age
func SetDataV4(name string, age xtype.OptionInt) {
	if age.Valid() {
		log.Print("set age", age.Unwrap())
	} else {
		log.Print("not set age")
	}
}

func main() {
	SetDataV1("goclub", 0)  // set age 0
	SetDataV1("goclub", 18) // set age 18

	SetDataV2("goclub", 18, false) // set age 18
	SetDataV2("goclub", 0, true)   // not set age

	agev3 := 18
	SetDataV3("goclub", &agev3) // set age 18
	SetDataV3("goclub", nil)    // not set age

	SetDataV4("goclub", xtype.Int(18))     // set age 18
	SetDataV4("goclub", xtype.OptionInt{}) // not set age
}
Output:

func (OptionInt) Unwrap

func (o OptionInt) Unwrap() int

func (OptionInt) Valid

func (o OptionInt) Valid() bool

type OptionInt16

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

func Int16

func Int16(i int16) OptionInt16

func (OptionInt16) Unwrap

func (o OptionInt16) Unwrap() int16

func (OptionInt16) Valid

func (o OptionInt16) Valid() bool

type OptionInt32

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

func Int32

func Int32(i int32) OptionInt32

func (OptionInt32) Unwrap

func (o OptionInt32) Unwrap() int32

func (OptionInt32) Valid

func (o OptionInt32) Valid() bool

type OptionInt64

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

func Int64

func Int64(i int64) OptionInt64

func (OptionInt64) Unwrap

func (o OptionInt64) Unwrap() int64

func (OptionInt64) Valid

func (o OptionInt64) Valid() bool

type OptionInt8

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

func Int8

func Int8(i int8) OptionInt8

func (OptionInt8) Unwrap

func (o OptionInt8) Unwrap() int8

func (OptionInt8) Valid

func (o OptionInt8) Valid() bool

type OptionRunes

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

func Runes

func Runes(runes []rune) OptionRunes

func (OptionRunes) Unwrap

func (o OptionRunes) Unwrap() []rune

func (OptionRunes) Valid

func (o OptionRunes) Valid() bool

type OptionString

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

func String

func String(s string) OptionString

func (OptionString) Unwrap

func (o OptionString) Unwrap() string

func (OptionString) Valid

func (o OptionString) Valid() bool

type OptionStrings

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

func Strings

func Strings(strings []string) OptionStrings

func (OptionStrings) Unwrap

func (o OptionStrings) Unwrap() []string

func (OptionStrings) Valid

func (o OptionStrings) Valid() bool

type OptionTime

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

func Time

func Time(t time.Time) OptionTime

func (OptionTime) Unwrap

func (o OptionTime) Unwrap() time.Time

func (OptionTime) Valid

func (o OptionTime) Valid() bool

type OptionUint

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

func Uint

func Uint(i uint) OptionUint

func (OptionUint) Unwrap

func (o OptionUint) Unwrap() uint

func (OptionUint) Valid

func (o OptionUint) Valid() bool

type OptionUint16

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

func Uint16

func Uint16(i uint16) OptionUint16

func (OptionUint16) Unwrap

func (o OptionUint16) Unwrap() uint16

func (OptionUint16) Valid

func (o OptionUint16) Valid() bool

type OptionUint32

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

func Uint32

func Uint32(i uint32) OptionUint32

func (OptionUint32) Unwrap

func (o OptionUint32) Unwrap() uint32

func (OptionUint32) Valid

func (o OptionUint32) Valid() bool

type OptionUint64

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

func Uint64

func Uint64(i uint64) OptionUint64

func (OptionUint64) Unwrap

func (o OptionUint64) Unwrap() uint64

func (OptionUint64) Valid

func (o OptionUint64) Valid() bool

type OptionUint8

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

func Uint8

func Uint8(i uint8) OptionUint8

func (OptionUint8) Unwrap

func (o OptionUint8) Unwrap() uint8

func (OptionUint8) Valid

func (o OptionUint8) Valid() bool

Jump to

Keyboard shortcuts

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