Skip to content

Easy-mode pointers to literals of primitives in Go.

License

Notifications You must be signed in to change notification settings

gplusplus314/pval

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pval = Pointer to Value

Since Go does not allow addressing a constant, and literal primitives are considered constants in this regard, it makes some code long-winded and noisy. I use this a lot in unit tests to keep them small and on-topic, but it's generally useful for areas of code where terseness is preferred over idiomatic grammar. I was annoyed, so I created this package.

Replace unnecessarily long code like the following:

var result *string

// ... do stuff

foo := "bar"
result = &foo

With this:

var result *string

// ... do stuff

result := pval.String("bar")

It's even worse if you happen to need a slice of pointers. Replace this eyesore:

var result []*string

// ... do stuff

foo1 := "bar1"
foo2 := "bar2"
foo3 := "bar3"
foo4 := "bar4"
result = append(result, &foo1, &foo2, &foo3, &foo4)

With this (the plural version):

var result []*string

// ... do stuff

result := pval.Strings("bar1", "bar2", "bar3", "bar4")

License

Creative Commons CC0 alt text

To the extent possible under law, Gerry Hernandez has waived all copyright and related or neighboring rights to pval.

About

Easy-mode pointers to literals of primitives in Go.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published