https://github.com/bavix/gonce
https://github.com/bavix/gonce
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/bavix/gonce
- Owner: bavix
- Created: 2021-03-27T08:18:50.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-02-20T18:14:51.000Z (over 2 years ago)
- Last Synced: 2025-10-28T06:27:03.954Z (9 months ago)
- Language: Go
- Size: 2.93 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
gonce - mini package implementing singleton.
* **Vendor**: bavix
* **Package**: gonce
### Installation
```bash
go get github.com/bavix/gonce
```
### Get started
let's make a simple singleton object for telegrams
```go
package telegram
import (
"github.com/bavix/gonce"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
var instance *tgbotapi.BotAPI
var once gonce.Once
func GetInstance(token string) *tgbotapi.BotAPI {
once.Do(func() {
botApi, err := tgbotapi.NewBotAPI(token)
if err != nil {
panic(err)
}
instance = botApi
})
return instance
}
```
PS, don't forget, singleton is an anti-pattern. It should be used extremely rarely.