1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00
Files
lancet/docs/pointer.md
2023-06-30 10:31:22 +08:00

1.8 KiB

Pointer

pointer contains some util functions to operate go pointer.

Source:

Usage:

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

Index

Documentation

Of

Returns a pointer to the pass value `v`.

Signature:

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

Example:

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

Returns the value from the pointer.

Signature:

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

Example:

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

Returns the underlying value by the given interface type

Signature:

func ExtractPointer(value any) any

Example:

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
}