ctxutils

package module
Version: v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2020 License: MIT Imports: 1 Imported by: 0

README

= ctxutils.

https://pkg.go.dev/github.com/ZergsLaw/ctxutils?tab=doc[image:https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square[go.dev
reference])]

== Link
:hide-uri-scheme:
If you have questions about this application, there was a bug or there are suggestions for improvement, then you can ask it in Issues, or in link:telegram[https://t.me/zergsLaw]

== Install.

----
go get -v github.com/ZergsLaw/ctxutils
----

== Utils for working with context and goroutines.

[source,go]
----
package main

import (
	"context"
	"log"
	"time"

	"github.com/ZergsLaw/ctxutils"
)

const timeout = time.Second

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()

	res := ""

	err := ctxutils.Go(ctx, func() error {
		var err error
		res, err = LongRunningProcess()
		if err != nil {
			return err
		}

		return nil
	})

	if err != nil {
		log.Fatal("very long process ", err)
	}

	log.Println(res)
}

func LongRunningProcess() (string, error) {
	time.Sleep(5 * time.Second)
	return "Hello, bro!", nil
}
----

Documentation

Overview

Package ctxutils contains utils and helpers for working with context.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Go

func Go(ctx context.Context, fn func() error) error

Go call function and monitors context if context is done, return context error else return result from callback.

Types

This section is empty.

Source Files

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL