Skip to content
/ ref Public

Implements functions for de-referencing primitive types

License

Notifications You must be signed in to change notification settings

trivigy/ref

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ref

CircleCI branch License GitHub tag (latest SemVer)

Introduction

In golang v1, unfortunately, there is no nice builtin way to represent optional struct fields for primitives whose zero value is individualized for each primitive. A lot of debating has been done and people hold different opinions as to what is the best solution. This library is my personal preferred way of dealing with the situation.

Example

package main

import (
	"fmt"
	
	"github.com/trivigy/ref"
)

type Sample struct {
	Field *string `json:"field,omitempty"`
}

func main() {
	result := Sample{
		Field: ref.String("example"),
	}
	
	if result.Field != nil {
		fmt.Print("'Field' is set")
	}
}

Alternatives

If you hold another opinion feel free to share it either via discord or on golang github channels. Or better yet share a PR with a new link added above.

About

Implements functions for de-referencing primitive types

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages