Читать книгу Practical Go - Amit Saha - Страница 61

Making Your Applications Robust

Оглавление

A hallmark of robust applications is that a certain level of control is enforced on their runtime behavior. For example, when your program makes an HTTP request, you may want it to complete within a user-specified number of seconds, and if not, exit with an error message. When such measures are enforced, the program's behavior is more predictable to the user. The context package in the standard library allows applications to enforce such control. It defines a Context struct type and three functions— withDeadline(), withCancel(), and withTimeout() —to enforce certain runtime guarantees around your code execution. You will find various standard library packages that require a context object to be passed as the first parameter. Some examples are functions in the net, net/http, and os/exec packages. Although use of contexts is most common when you are communicating with external resources, they are certainly equally applicable to any other functionality where there may be a chance of unpredictable behavior.

Practical Go

Подняться наверх