https://github.com/aleksi/pointer
Go package pointer provides helpers to get pointers to values of built-in types.
https://github.com/aleksi/pointer
hacktoberfest
Last synced: about 1 year ago
JSON representation
Go package pointer provides helpers to get pointers to values of built-in types.
- Host: GitHub
- URL: https://github.com/aleksi/pointer
- Owner: AlekSi
- License: mit
- Created: 2014-06-25T12:23:26.000Z (about 12 years ago)
- Default Branch: main
- Last Pushed: 2022-11-01T18:36:01.000Z (over 3 years ago)
- Last Synced: 2025-05-15T23:06:26.851Z (about 1 year ago)
- Topics: hacktoberfest
- Language: Go
- Homepage:
- Size: 22.5 KB
- Stars: 222
- Watchers: 7
- Forks: 19
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pointer
[](https://pkg.go.dev/github.com/AlekSi/pointer)
Go package `pointer` provides helpers to convert between pointers and values of built-in
(and, with Go 1.18+ generics, of any) types.
```
go get github.com/AlekSi/pointer
```
API is stable. [Documentation](https://pkg.go.dev/github.com/AlekSi/pointer).
```go
package motivationalexample
import (
"encoding/json"
"github.com/AlekSi/pointer"
)
const (
defaultName = "some name"
)
// Stuff contains optional fields.
type Stuff struct {
Name *string
Comment *string
Value *int64
Time *time.Time
}
// SomeStuff makes some JSON-encoded stuff.
func SomeStuff() (data []byte, err error) {
return json.Marshal(&Stuff{
Name: pointer.ToString(defaultName), // can't say &defaultName
Comment: pointer.ToString("not yet"), // can't say &"not yet"
Value: pointer.ToInt64(42), // can't say &42 or &int64(42)
Time: pointer.ToTime(time.Date(2014, 6, 25, 12, 24, 40, 0, time.UTC)), // can't say &time.Date(…)
})
}
```