ptrKit

package
v3.0.906 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertNotNilAndIsPointer

func AssertNotNilAndIsPointer(ptr interface{}) error

AssertNotNilAndIsPointer

合法的传参: (1) ptr != nil(值非nil) (2) ptr的类型为"指针"

func ExtractPointer

func ExtractPointer(value any) any

ExtractPointer 返回传入interface的底层值.

func IsPointer

func IsPointer(obj interface{}) bool

IsPointer 传参的类型是否为指针?

PS: 传参的值无影响,即使其为nil.

@param obj 可以为nil

e.g.

type bean struct {
}
var b *bean = nil
fmt.Println(ptrKit.IsPointer(b)) 				// true(类型为指针,虽然值为nil)

fmt.Println(ptrKit.IsPointer(interface{}(nil))) // false

func IsPointer1

func IsPointer1(obj interface{}) bool

IsPointer1 *

  • 判断参数obj是否为指针(pointer).
  • 参考:github.com/json-iterator/go => reflect.go => func (iter *Iterator) ReadVal(obj interface{}) *
  • @param obj nil => false

func IsPointer2

func IsPointer2(obj interface{}) bool

IsPointer2

e.g. (nil) => false

func Of

func Of[T any](v T) *T

Of 返回传入参数的指针值.

@param v 可以为nil

e.g.

var ptr *string = ptrKit.Of("123")
fmt.Println(ptr) // 0x14000110360

e.g.1 传参v可以为nil

var obj interface{} = nil
fmt.Println(ptrKit.Of(obj)) // 0x1400008e380

var b *bean = nil
fmt.Println(ptrKit.Of(b)) // 0x140000980c0

var obj1 interface{} = b
fmt.Println(ptrKit.Of(obj1)) // 0x1400008e390

func ToString

func ToString(ptr interface{}) string

ToString 指针 => 指针的地址字符串(十六进制表示,前缀 0x)

e.g.

tmp := 1
(&tmp) => "0xc00000a228"

e.g.1

(nil) => "%!p(<nil>)"

func UnwarpOr

func UnwarpOr[T any](p *T, fallback T) T

UnwarpOr 返回指针的值(如果指针为零值,则返回fallback).

@param p 可以为nil(将返回 fallback)

e.g.

var ptr *int = nil
fmt.Println(ptrKit.UnwarpOr(ptr, 123)) // 123

func UnwarpOrDefault

func UnwarpOrDefault[T any](p *T) T

UnwarpOrDefault 返回指针的值(如果指针为零值,则返回相应类型的零值).

@param p 可以为nil(将返回 相应类型的零值)

e.g.

var ptr *int = nil
fmt.Println(ptrKit.UnwarpOrDefault(ptr)) // 0

func Unwrap

func Unwrap[T any](p *T) T

Unwrap 返回指针的值.

@param p 不能为nil(否则会panic)

e.g.

var ptr *string = ptrKit.Of("123")
fmt.Println(ptr) // 0x140000103b0

fmt.Println(ptrKit.Unwrap(ptr)) // 123

e.g.1

var ptr *string = nil
fmt.Println(ptrKit.Unwrap(ptr)) // panic: runtime error: invalid memory address or nil pointer dereference

Types

This section is empty.

Jump to

Keyboard shortcuts

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