Categorygithub.com/golang-infrastructure/go-pointer
modulepackage
0.0.5
Repository: https://github.com/golang-infrastructure/go-pointer.git
Documentation: pkg.go.dev

# README

Go Pointer

中文文档 English Document

一、引入依赖

go get -u github.com/golang-infrastructure/go-pointer

二、解决了什么问题

在golang中基本类型因为没有包装类型,这就导致基本类型无法区分出nil和零值,于是所以很多库都倾向于采用基本类型变量的指针来区分是没有传递还是传递了零值。

一个具体的例子,当不使用指针的时候,在执行任务的时候有个配置项:

package main

type Config struct {
	Foo int
}

当Foo的值为0的时候我们不知道是传递了零值还是没有传递值,因为有些地方是要比较细的区分这些情况的,这个时候一些库就倾向于采用指针类型:

package main

type Config struct {
	Foo *int
}

但是有时候这个值就是一个字面值常量传进去的,比如查询数据库时的分页大小等,这个时候如果要获取指针类型的话就有点麻烦,比如:

package main 

func main() {
    
    foo := 10 
    config := &Config{
        Foo: &foo, 
    }
    callSomeFunction(config)
    
}

如果使用这个库的话:

package main 

func main() {
    
    config := &Config{
        Foo: pointer.ToPointer(10) 
    }
    callSomeFunction(config)
    
}

Diff:

package main 

func main() {

-	foo := 10 
    config := &Config{
-    	Foo: &foo, 
+       Foo: pointer.ToPointer(10) 
    }
    callSomeFunction(config)
    
}

上面这个场景只是举了一个例子,这个模块就是用来解决类似的问题的。

三、Example Code

目前已经支持泛型:

package main

import (
	"fmt"
	"github.com/golang-infrastructure/go-pointer"
)

func main() {

	// 返回false的指针
	falsePointer := pointer.FalsePointer()
	fmt.Println(fmt.Sprintf("%T %v", falsePointer, *falsePointer)) // Output: *bool false

	// 返回true的指针
	truePointer := pointer.TruePointer()
	fmt.Println(fmt.Sprintf("%T %v", truePointer, *truePointer)) // Output: *bool true

	// 返回对应类型的指针
	v1 := 1
	toPointer := pointer.ToPointer(v1)
	fmt.Println(fmt.Sprintf("%T %v", toPointer, *toPointer)) // Output: *int 1
	// 返回对应类型的指针,但是会检查值,如果值为对应类型的零值的话则返回nil
	v1 = 0
	orNil := pointer.ToPointerOrNil(v1)
	fmt.Println(orNil) // Output: nil

	// 从指针读取值 ,如果为nil指针的话则返回对应类型的零值
	v2 := 1
	v3 := &v2
	fromPointer := pointer.FromPointer(v3)
	fmt.Println(fromPointer) // Output: 1
	// 从指针读取值,如果是nil指针的话则返回给定的默认值
	v2 = 0
	orDefault := pointer.FromPointerOrDefault(v3, 1)
	fmt.Println(orDefault) // Output: 0

	// 返回当前时间的指针
	nowPointer := pointer.Now()
	fmt.Println(nowPointer) // Output: 2023-05-30 11:46:20.3695476 +0800 CST m=+0.003922101

	// atomic.Bool指针 - true
	atomicTruePointer := pointer.AtomicTruePointer()
	fmt.Println(atomicTruePointer) // Output: &{{} 1}

	// atomic.Bool指针 - false
	atomicFalsePointer := pointer.AtomicFalsePointer()
	fmt.Println(atomicFalsePointer) // Output: &{{} 0}

	// atomic.Int64指针
	int64Pointer := pointer.AtomicInt64Pointer(128)
	fmt.Println(int64Pointer) // Output: &{{} {} 128}

}

# Packages

No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
FalsePointer 返回一个布尔指针,其值为false.
FromPointer 获取指针实际指向的值,如果指针为nil的话则返回对应类型的零值.
FromPointerOrDefault 获取指针实际指向的值,如果指针为nil的话则返回给定的defaultValue.
Now 返回当前时间的指针.
ToPointer 将布尔变量转换为布尔指针.
ToPointerOrNil 转换为对应类型的指针,如果变量的值为对应类型的零值的话则转换为nil指针,如果是struct,则每个字段都为零值.
TruePointer 返回一个布尔指针,其值为true.