Git Product home page Git Product logo

go-pointer's Introduction

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}

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.