Skip to content
/ nilx Public

nilx is a library that provides utility functions to easily handle nil pointers in Go.

License

Notifications You must be signed in to change notification settings

otakakot/nilx

Repository files navigation

nilx

nilx is a library that provides utility functions to easily handle nil pointers in Go.

Installation

go get github.com/otakakot/nilx

Usage

NilOr

The NilOr function returns the first non-nil value from multiple pointers. If all pointers are nil, it returns the zero value of the type.

package main

import (
    "fmt"

    "github.com/otakakot/nilx"
)

func main() {
    val := 1
    result := nilx.NilOr(nil, &val)
    fmt.Println(result) // Output: 1
}

NilZero

The NilZero function returns the zero value of the type if the pointer is nil, otherwise it returns the value of the pointer.

package main

import (
    "fmt"

    "github.com/otakakot/nilx"
)

func main() {
    result := nilx.NilZero[int](nil)
    fmt.Println(result) // Output: 0
}

NilDef

The NilDef function returns the default value if the pointer is nil, otherwise it returns the value of the pointer.

package main

import (
    "fmt"

    "github.com/otakakot/nilx"
)

func main() {
    result := nilx.NilDef(nil, 1)
    fmt.Println(result) // Output: 1
}

License

This project is licensed under the Apache License 2.0.

About

nilx is a library that provides utility functions to easily handle nil pointers in Go.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages