Skip to content
本页内容

# Pointer ​

pointer 包支持一些指针类型的操作。

## 源码: ​

- https://github.com/duke-git/lancet/blob/main/pointer/pointer.go

## 用法: ​

go

    import (
        "github.com/duke-git/lancet/v2/pointer"
    )

    import (
        "github.com/duke-git/lancet/v2/pointer"
    )

## 目录 ​

- Of
- Unwrap
- ExtractPointer
- UnwarpOr
- UnwarpOrDefault

## 文档 ​

### Of ​

返回传入参数的指针值。

函数签名:

go

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

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

示例:运行

go

    package main

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/pointer"
    )

    func main() {
        result1 := pointer.Of(123)
        result2 := pointer.Of("abc")

        fmt.Println(*result1)
        fmt.Println(*result2)

        // Output:
        // 123
        // abc
    }

    package main

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/pointer"
    )

    func main() {
        result1 := pointer.Of(123)
        result2 := pointer.Of("abc")

        fmt.Println(*result1)
        fmt.Println(*result2)

        // Output:
        // 123
        // abc
    }

### Unwrap ​

返回传入指针指向的值。

函数签名:

go

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

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

示例:运行

go

    package main

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/pointer"
    )

    func main() {
        a := 123
        b := "abc"

        result1 := pointer.Unwrap(&a)
        result2 := pointer.Unwrap(&b)

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // 123
        // abc
    }

    package main

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/pointer"
    )

    func main() {
        a := 123
        b := "abc"

        result1 := pointer.Unwrap(&a)
        result2 := pointer.Unwrap(&b)

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // 123
        // abc
    }

### ExtractPointer ​

返回传入interface的底层值。

函数签名:

go

    func ExtractPointer(value any) any

    func ExtractPointer(value any) any

示例:运行

go

    package main

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/pointer"
    )

    func main() {
        a := 1
        b := &a
        c := &b
        d := &c

        result := pointer.ExtractPointer(d)

        fmt.Println(result)

        // Output:
        // 1
    }

    package main

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/pointer"
    )

    func main() {
        a := 1
        b := &a
        c := &b
        d := &c

        result := pointer.ExtractPointer(d)

        fmt.Println(result)

        // Output:
        // 1
    }

### UnwarpOr ​

返回指针的值，如果指针为零值，则返回fallback。

函数签名:

go

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

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

示例:运行

go

    package main

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/pointer"
    )

    func main() {
    	a := 123
    	b := "abc"

    	var c *int
    	var d *string

    	result1 := pointer.UnwarpOr(&a, 456)
    	result2 := pointer.UnwarpOr(&b, "abc")
    	result3 := pointer.UnwarpOr(c, 456)
    	result4 := pointer.UnwarpOr(d, "def")

    	fmt.Println(result1)
    	fmt.Println(result2)
    	fmt.Println(result3)
    	fmt.Println(result4)

    	// Output:
    	// 123
    	// abc
    	// 456
    	// def
    }

    package main

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/pointer"
    )

    func main() {
    	a := 123
    	b := "abc"

    	var c *int
    	var d *string

    	result1 := pointer.UnwarpOr(&a, 456)
    	result2 := pointer.UnwarpOr(&b, "abc")
    	result3 := pointer.UnwarpOr(c, 456)
    	result4 := pointer.UnwarpOr(d, "def")

    	fmt.Println(result1)
    	fmt.Println(result2)
    	fmt.Println(result3)
    	fmt.Println(result4)

    	// Output:
    	// 123
    	// abc
    	// 456
    	// def
    }

### UnwarpOrDefault ​

返回指针的值，如果指针为零值，则返回相应零值。

函数签名:

go

    UnwarpOrDefault[T any](p *T) T

    UnwarpOrDefault[T any](p *T) T

示例:运行

go

    package main

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/pointer"
    )

    func main() {
    	a := 123
    	b := "abc"

    	var c *int
    	var d *string

    	result1 := pointer.UnwarpOrDefault(&a)
    	result2 := pointer.UnwarpOrDefault(&b)
    	result3 := pointer.UnwarpOrDefault(c)
    	result4 := pointer.UnwarpOrDefault(d)

    	fmt.Println(result1)
    	fmt.Println(result2)
    	fmt.Println(result3)
    	fmt.Println(result4)

    	// Output:
    	// 123
    	// abc
    	// 0
    	//
    }

    package main

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/pointer"
    )

    func main() {
    	a := 123
    	b := "abc"

    	var c *int
    	var d *string

    	result1 := pointer.UnwarpOrDefault(&a)
    	result2 := pointer.UnwarpOrDefault(&b)
    	result3 := pointer.UnwarpOrDefault(c)
    	result4 := pointer.UnwarpOrDefault(d)

    	fmt.Println(result1)
    	fmt.Println(result2)
    	fmt.Println(result3)
    	fmt.Println(result4)

    	// Output:
    	// 123
    	// abc
    	// 0
    	//
    }