ptrKit

package
v2.0.13 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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

e.g. (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