singleflight

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2024 License: MIT Imports: 2 Imported by: 0

README

singleflight

This is merely a wrapper for x/sync/singleflight, removing the annoying type assertions when in use.

Install

go get github.com/aico-io/singleflight@v0.1.0

Example usage

package main

import (
	"fmt"
	"github.com/aico-io/singleflight"
)

type User struct {
	Name string
}

func (u *User) GetName() string { return u.Name }

type Cat struct {
	Color string
}

func (c *Cat) GetColor() string { return c.Color }

func main() {
	u, err, _ := singleflight.Do[User]("key", func() (any, error) { return User{"Zephyr"}, nil })
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(u.GetName())
	
	c, err, _ := singleflight.Do[Cat]("key", func() (any, error) { return Cat{"Black"}, nil })
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(c.GetColor())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do[T any](key string, fn func() (any, error)) (v T, err error, shared bool)

func DoChan

func DoChan[T any](key string, fn func() (any, error)) <-chan Result[T]

func Forget

func Forget(key string)

Types

type Result

type Result[T any] struct {
	Val    T
	Err    error
	Shared bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL