ptr

package
v2.0.0-...-e41ac6a Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: MIT Imports: 1 Imported by: 1

README

ptr Library

The ptr library provides utility functions for creating pointers to primitive types in Go. This is particularly useful when you need to pass a pointer to a literal value or when working with APIs that require pointers.

Installation

import "github.com/getevo/evo/v2/lib/ptr"

Features

  • Primitive Type Pointers: Create pointers to all Go primitive types
  • Numeric Types: Support for all integer and floating-point types
  • String Pointers: Create pointers to string values
  • Boolean Pointers: Create pointers to boolean values
  • Time Pointers: Create pointers to time.Time values
  • Interface Pointers: Create pointers to interface{} values

Usage Examples

Basic Usage
package main

import (
    "fmt"
    "github.com/getevo/evo/v2/lib/ptr"
    "time"
)

func main() {
    // Create pointers to primitive types
    intPtr := ptr.Int(42)
    stringPtr := ptr.String("hello")
    boolPtr := ptr.Bool(true)
    floatPtr := ptr.Float64(3.14)
    timePtr := ptr.Time(time.Now())
    
    // Use the pointers
    fmt.Printf("Int value: %d\n", *intPtr)
    fmt.Printf("String value: %s\n", *stringPtr)
    fmt.Printf("Bool value: %t\n", *boolPtr)
    fmt.Printf("Float value: %f\n", *floatPtr)
    fmt.Printf("Time value: %v\n", *timePtr)
}
Using with Struct Fields
package main

import (
    "fmt"
    "github.com/getevo/evo/v2/lib/ptr"
)

type User struct {
    ID        int
    Name      string
    Age       *int       // Optional field
    Email     *string    // Optional field
    IsActive  *bool      // Optional field
}

func main() {
    // Create a user with all fields
    user1 := User{
        ID:       1,
        Name:     "John",
        Age:      ptr.Int(30),
        Email:    ptr.String("john@example.com"),
        IsActive: ptr.Bool(true),
    }
    
    // Create a user with only required fields
    user2 := User{
        ID:   2,
        Name: "Jane",
        // Age, Email, and IsActive are nil
    }
    
    // Check if optional fields are set
    if user1.Age != nil {
        fmt.Printf("User1 age: %d\n", *user1.Age)
    }
    
    if user2.Age != nil {
        fmt.Printf("User2 age: %d\n", *user2.Age)
    } else {
        fmt.Println("User2 age not set")
    }
}
Using with Function Parameters
package main

import (
    "fmt"
    "github.com/getevo/evo/v2/lib/ptr"
)

// Function that takes optional parameters as pointers
func updateUser(id int, name *string, age *int, isActive *bool) {
    fmt.Printf("Updating user %d\n", id)
    
    if name != nil {
        fmt.Printf("New name: %s\n", *name)
    }
    
    if age != nil {
        fmt.Printf("New age: %d\n", *age)
    }
    
    if isActive != nil {
        fmt.Printf("New active status: %t\n", *isActive)
    }
}

func main() {
    // Update only the name
    updateUser(1, ptr.String("John Doe"), nil, nil)
    
    // Update name and age
    updateUser(2, ptr.String("Jane Doe"), ptr.Int(25), nil)
    
    // Update all fields
    updateUser(3, ptr.String("Bob Smith"), ptr.Int(40), ptr.Bool(false))
}

How It Works

The ptr library provides a set of simple functions, each taking a value of a specific type and returning a pointer to that value. For example, ptr.Int(42) creates a new int with the value 42 and returns a pointer to it.

This is equivalent to the following Go code:

func Int(v int) *int {
    return &v
}

The library includes similar functions for all primitive types in Go, making it easy to create pointers to literal values without having to declare variables first.

For more detailed information, please refer to the source code and comments within the library.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(v bool) *bool

Bool returns a pointer to the given bool value.

func Float32

func Float32(v float32) *float32

Float32 returns a pointer to the given float32 value.

func Float64

func Float64(v float64) *float64

Float64 returns a pointer to the given float64 value.

func Int

func Int(v int) *int

Int returns a pointer to the given int value.

func Int8

func Int8(v int8) *int8

Int8 returns a pointer to the given int8 value.

func Int16

func Int16(v int16) *int16

Int16 returns a pointer to the given int16 value.

func Int32

func Int32(v int32) *int32

Int32 returns a pointer to the given int32 value.

func Int64

func Int64(v int64) *int64

Int64 returns a pointer to the given int64 value.

func Interface

func Interface(v interface{}) *interface{}

Interface returns a pointer to the given interface{} value.

func String

func String(v string) *string

String returns a pointer to the given string value.

func Time

func Time(v time.Time) *time.Time

Time returns a pointer to the given time.Time value.

func Uint

func Uint(v uint) *uint

Uint returns a pointer to the given uint value.

func Uint8

func Uint8(v uint8) *uint8

Uint8 returns a pointer to the given uint8 value.

func Uint16

func Uint16(v uint16) *uint16

Uint16 returns a pointer to the given uint16 value.

func Uint32

func Uint32(v uint32) *uint32

Uint32 returns a pointer to the given uint32 value.

func Uint64

func Uint64(v uint64) *uint64

Uint64 returns a pointer to the given uint64 value.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL