Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thediveo/once
improves your testing feng shui tremendiously by executing a function only once that might need to be triggered from multiple call sites and not just from a single defer.
https://github.com/thediveo/once
Last synced: about 1 month ago
JSON representation
improves your testing feng shui tremendiously by executing a function only once that might need to be triggered from multiple call sites and not just from a single defer.
- Host: GitHub
- URL: https://github.com/thediveo/once
- Owner: thediveo
- License: apache-2.0
- Created: 2023-04-06T11:06:29.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-10-26T18:00:05.000Z (about 1 year ago)
- Last Synced: 2024-05-16T14:01:50.748Z (6 months ago)
- Language: Shell
- Size: 14.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `once`
[![Go Reference](https://pkg.go.dev/badge/github.com/thediveo/once.svg)](https://pkg.go.dev/github.com/thediveo/once)
![GitHub](https://img.shields.io/github/license/thediveo/once)
![build and test](https://github.com/TheDiveO/once/workflows/build%20and%20test/badge.svg?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/thediveo/once)](https://goreportcard.com/report/github.com/thediveo/once)
![Coverage](https://img.shields.io/badge/Coverage-100.0%25-brightgreen)This tiny Go module improves your testing feng shui tremendiously by executing a
function only once that might need to be triggered from _multiple_ call sites
and not just from a _single_ `defer` call site.Type `Oncer` works like `sync.Once`, except that the function to execute once is
specified only, _erm_, once when creating a `Oncer`.#### Before
```go
rsc, _ := somemod.Open()
// Make sure to always clean up, but only once as
// somemod panics on multiple Close()s.
var once sync.Once
defer once.Do(func() { rsc.Close() })
// Do test something
// ...
once.Do(func() { rsc.Close() })
// Do test something else then f has been closed
// ...
```#### After
```go
// You might want to dot-import for convenience.
import . "github.com/thediveo/once"rsc, _ := somemod.Open()
// Make sure to always clean up, but only once as
// somemod panics on multiple Close()s.
once := Once(func() { rsc.Close() })
defer once.Do()
// Do test something
// ...
once.Do()
// Do test something else then f has been closed
// ...
```## Go Version Support
`once` supports versions of Go that are noted by the [Go release
policy](https://golang.org/doc/devel/release.html#policy), that is, major
versions _N_ and _N_-1 (where _N_ is the current major version).## Copyright and License
`once` is Copyright 2023 Harald Albrecht, and licensed under the Apache
License, Version 2.0.