ptrKit

package
v2.8.122 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2023 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 FromPtr

func FromPtr[T any](x *T) T

FromPtr 返回指针的值(如果指针为nil则返回T类型的零值).

@param x 可以为nil(此时将返回类型T的zero value) @return 可能为nil

e.g.

str := "测试test"
value := ptrKit.FromPtr(&str)
fmt.Println(value) // "测试test"
value = ptrKit.FromPtr[string](nil)
fmt.Println(value) // ""

func FromPtrOr

func FromPtrOr[T any](x *T, fallback T) T

FromPtrOr 返回指针的值(如果指针为nil则返回传参fallback).

@param x 可以为nil(此时将返回传参fallback)

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 ToPtr

func ToPtr[T any](x T) *T

ToPtr Returns a pointer copy of value.

@param x 可以为nil(但需要指定类型T,且这么干貌似没意义)

e.g.

ptr := ptrKit.ToPtr("hello world")
fmt.Println(ptr) 	// 0x140000105e0

e.g.1 x == nil的情况

ptr := ptrKit.ToPtr[interface{}](nil)
fmt.Println(ptr)                     // 0x1400010c5c0
fmt.Println(ptr == nil)              // false
fmt.Println(interfaceKit.IsNil(ptr)) // false

func ToString

func ToString(ptr interface{}) string

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

e.g.

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

e.g.1

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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