https://github.com/huandu/go-singleton
Generic singleton for Go.
https://github.com/huandu/go-singleton
generics go singleton
Last synced: 4 months ago
JSON representation
Generic singleton for Go.
- Host: GitHub
- URL: https://github.com/huandu/go-singleton
- Owner: huandu
- License: mit
- Created: 2020-07-14T04:35:15.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-03-16T03:28:15.000Z (over 3 years ago)
- Last Synced: 2025-01-30T02:45:06.721Z (6 months ago)
- Topics: generics, go, singleton
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-singleton: Generic singleton factory
[](https://github.com/huandu/go-singleton/actions)
[](https://pkg.go.dev/github.com/huandu/go-singleton)Starting from go1.18, Go will officially support generic. This package is designed to embrace the new technic for convenience.
## Usage
Call `Singleton` with a given type name to get a pointer to this type.
```go
type MyType struct {
field int
}v1 := singleton.Singleton[MyType]()
v1.field = 123v2 := singleton.Singleton[MyType]()
println(v2.field) // Output: 123
```