• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.gitignoreH A D20-May-2019192

LICENSEH A D20-May-20191 KiB

README.mdH A D20-May-20193.2 KiB

go.modH A D20-May-2019180

go.sumH A D20-May-2019521

pointy.goH A D20-May-20195.5 KiB

pointy_test.goH A D20-May-20196.5 KiB

README.md

1# pointy
2
3Simple helper functions to provide a shorthand to get a pointer to a variable holding a constant...because it's annoying when you have to do it hundreds of times in unit tests:
4
5```golang
6val := 42
7pointerToVal := &val
8// vs.
9pointerToVal := pointy.Int(42)
10```
11
12**New in release 1.1.0**
13
14Additional helper functions have been added to safely dereference pointers
15or return a fallback value:
16
17```golang
18val := 42
19pointerToVal := &val
20// then later in your code..
21myVal := pointy.IntValue(pointerToVal, 99) // returns 42 (or 99 if pointerToVal was nil)
22```
23
24## GoDoc
25
26https://godoc.org/github.com/mwielbut/pointy
27
28## Installation
29
30`go get github.com/mwielbut/pointy`
31
32## Example
33
34```golang
35package main
36
37import (
38	"fmt"
39
40	"github.com/mwielbut/pointy"
41)
42
43func main() {
44	foo := pointy.Int64(2018)
45	fmt.Println("foo is a pointer to:", *foo)
46
47	bar := pointy.String("point to me")
48	fmt.Println("bar is a pointer to:", *bar)
49
50	// get the value back out (new in v1.1.0)
51	barVal := pointy.StringValue(bar, "empty!")
52	fmt.Println("bar's value is:", barVal)
53}
54```
55
56## Available Functions
57
58`Bool(x bool) *bool`
59`BoolValue(p *bool, fallback bool) bool`
60`Byte(x byte) *byte`
61`ByteValue(p *byte, fallback byte) byte `
62`Complex128(x complex128) *complex128`
63`Complex128Value(p *complex128, fallback complex128) complex128`
64`Complex64(x complex64) *complex64`
65`Complex64Value(p *complex64, fallback complex64) complex64`
66`Float32(x float32) *float32`
67`Float32Value(p *float32, fallback float32) float32`
68`Float64(x float64) *float64`
69`Float64Value(p *float64, fallback float64) float64`
70`Int(x int) *int`
71`IntValue(p *int, fallback int) int`
72`Int8(x int8) *int8`
73`Int8Value(p *int8, fallback int8) int8`
74`Int16(x int16) *int16`
75`Int16Value(p *int16, fallback int16) int16`
76`Int32(x int32) *int32`
77`Int32Value(p *int32, fallback int32) int32`
78`Int64(x int64) *int64`
79`Int64Value(p *int64, fallback int64) int64`
80`Uint(x uint) *uint`
81`UintValue(p *uint, fallback uint) uint`
82`Uint8(x uint8) *uint8`
83`Uint8Value(p *uint8, fallback uint8) uint8`
84`Uint16(x uint16) *uint16`
85`Uint16Value(p *uint16, fallback uint16) uint16`
86`Uint32(x uint32) *uint32`
87`Uint32Value(p *uint32, fallback uint32) uint32`
88`Uint64(x uint64) *uint64`
89`Uint64Value(p *uint64, fallback uint64) uint64`
90`String(x string) *string`
91`StringValue(p *string, fallback string) string`
92`Rune(x rune) *rune`
93`RuneValue(p *rune, fallback rune) rune`
94
95## Motivation
96
97Creating pointers to literal constant values is useful, especially in unit tests. Go doesn't support simply using the address operator (&) to reference the location of e.g. `value := &int64(42)` so we're forced to [create](https://stackoverflow.com/questions/35146286/find-address-of-constant-in-go/35146856#35146856) [little](https://stackoverflow.com/questions/34197248/how-can-i-store-reference-to-the-result-of-an-operation-in-go/34197367#34197367) [workarounds](https://stackoverflow.com/questions/30716354/how-do-i-do-a-literal-int64-in-go/30716481#30716481). A common solution is to create a helper function:
98
99```golang
100func createInt64Pointer(x int64) *int64 {
101    return &x
102}
103// now you can create a pointer to 42 inline
104value := createInt64Pointer(42)
105```
106
107This package provides a library of these simple little helper functions for every native Go primitive.
108