The only generic function you need for pointerizing literals in Go.
Go is amazing, but it makes one thing surprisingly awkward:
// You can't do this:
x := &"hello" // 🚫 compile errorYou either:
- Make a temp variable like a caveman, or
- Write the same boilerplate helper in every repo.
Let’s fix that:
import "github.com/funnelstory/ptr"
x := ptr.Ptr("hello") // ✅ *stringThat’s it. One function. Fully generic. Reusable anywhere. Test-covered. Type-safe.
go get github.com/funnelstory/ptrOr if you're using modules, just import it and go mod tidy will do the rest.
The ptr package exports exactly one thing:
func Ptr[T any](v T) *Tpackage main
import (
"fmt"
"github.com/funnelstory/ptr"
)
func main() {
s := ptr.Ptr("golang")
fmt.Println(*s) // Output: golang
}ptr.Ptr(42) // *int
ptr.Ptr(true) // *bool
ptr.Ptr(3.14) // *float64
ptr.Ptr([]string{"a", "b"}) // *[]string
ptr.Ptr(map[string]int{}) // *map[string]int
ptr.Ptr(func() {}) // *func()
ptr.Ptr(nil) // *any- Avoid boilerplate
tmp := val; &tmp - Helpful in:
- JSON/YAML marshaling with optional
*Tfields - Config structs
- Unit testing
- Working with APIs that use pointer types for optionality
- JSON/YAML marshaling with optional
- Tiny and dependency-free
- Idiomatic: one package, one purpose
ptr/
├── ptr/
│ └── ptr.go # <- The one true function
│ └── ptr_test.go # <- Tests covering many types
├── go.mod
├── Makefile # <- test, lint, etc.
├── .github/
│ └── workflows/
│ └── ci.yml # <- GitHub Actions for lint + test
go test ./...Or use the Makefile:
make testTests are parallelized and include:
- Primitives
- Structs, maps, slices
- Nested pointers
- Interface and function values
- Nilables
- Requires Go 1.18+ (uses generics)
- Works with all built-in and user-defined types
- 100% test coverage
This is a micro-library, but you’re welcome to contribute:
- Better docs/examples
- More weird test cases
- A
Deref[T]companion (?) - Benchmarks
Open an issue or PR — and please include tests!