Загрузка...

Golang | Overview of context Package in Go

Overview of context Package in Go
Purpose: The context package in Go provides a way to pass request-scoped values, deadlines, and cancellation signals between different parts of an application.

Context Propagation: It is mainly used to control the lifecycle of processes, ensuring that functions can stop execution when they are no longer needed, such as in web servers, background jobs, etc.
Types of Context

Background Context:

Created using context.Background().

Used as a top-level context, usually for main functions or initialization.
TODO Context:
Created using context.TODO().
Used when you are unsure about which context to use, acting as a placeholder.

Context Cancellation
Purpose: Cancellation allows a process to stop execution early when it's no longer needed, preventing resource leaks.
Usage:

Use context.WithCancel(parentContext) to create a cancelable context.
The function returns a new Context and a cancel() function.
When cancel() is called, all functions that are listening on that context are notified to stop execution.

Видео Golang | Overview of context Package in Go канала Rohit Jadhav
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять