Skip to content

The utility to help you avoid panic when accessing value from pointer address if nil

License

Notifications You must be signed in to change notification settings

riskiamad/nilsafer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NILSAFER

Nilsafer is a utility to help you get value from the pointer address without worrying with panic. Nilsafer will return zero value or default value from its type if the pointer address is nil.

Installation

go get -u github.com/riskiamad/nilsafer

Getting Started

Simple Usage ValueOrZero

package main

import (
	"fmt"
	"github.com/riskiamad/nilsafer"
)

func main() {
	var s = "hello"
	var nilString *string = nil

	fmt.Println(nilsafer.ValueOrZero(&s))
	fmt.Println(nilsafer.ValueOrZero(nilString))
}

/*output:
hello

 */

Simple Usage ValueOrDefault

package main

import (
	"fmt"
	"github.com/riskiamad/nilsafer"
)

func main() {
	var s = "hello"
	var nilString *string = nil
	var d = "world"

	fmt.Println(nilsafer.ValueOrDefault(&s, d))
	fmt.Println(nilsafer.ValueOrDefault(nilString, d))
}

/*output:
hello
world
 */

About

The utility to help you avoid panic when accessing value from pointer address if nil

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages