contextutil

package
v0.0.0-...-cd3ace8 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsDone

func IsDone(ctx context.Context) bool

IsDone returns ctx is already done or else.

Example
package main

import (
	"fmt"

	"go.nanasi880.dev/x/context/contextutil"
)

func main() {

	ctx, cancel := contextutil.NewCancel()

	fmt.Println(contextutil.IsDone(ctx))
	cancel()
	fmt.Println(contextutil.IsDone(ctx))

}
Output:

false
true

func IsError

func IsError(err error) bool

IsError returns true, if err is context.Canceled or context.DeadlineExceeded.

Example
package main

import (
	"context"
	"fmt"

	"go.nanasi880.dev/x/context/contextutil"
)

func main() {

	fmt.Println(contextutil.IsError(nil))
	fmt.Println(contextutil.IsError(context.Canceled))
	fmt.Println(contextutil.IsError(context.DeadlineExceeded))
	fmt.Println(contextutil.IsError(fmt.Errorf("context canceled")))
}
Output:

false
true
true
false

func NewCancel

func NewCancel() (context.Context, context.CancelFunc)

NewCancel is shorthand of context.WithCancel(context.Background()).

Types

type CancelableContext

type CancelableContext interface {
	context.Context
	Cancel()
	IsDone() bool
}

CancelableContext is cancelable context.

Example
package main

import (
	"fmt"

	"go.nanasi880.dev/x/context/contextutil"
)

func main() {
	ctx := contextutil.NewCancelable()

	fmt.Println(ctx.IsDone())
	ctx.Cancel()
	fmt.Println(ctx.IsDone())

}
Output:

false
true

func NewCancelable

func NewCancelable() CancelableContext

NewCancelable is shorthand of NewCancelableContext(context.Background()).

func NewCancelableContext

func NewCancelableContext(parent context.Context) CancelableContext

NewCancelableContext is creating new cancelable context with parent context.

Jump to

Keyboard shortcuts

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