Skip to content

Commit

Permalink
[CONTEXT] adds context theory to the slides
Browse files Browse the repository at this point in the history
  • Loading branch information
PatAkil committed Nov 4, 2024
1 parent cb067cd commit b4c6b05
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Binary file added examples/images/context.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions go-training.slide
Original file line number Diff line number Diff line change
Expand Up @@ -1600,6 +1600,50 @@ Tasks:
Peek:solutions/concurrency3


#----------------------------------------------
* Context
#----------------------------------------------
* Context

The context type carries deadlines, cancellation signals, across API boundaries and between processes.

- Incoming requests to a server should create a Context.

- Outgoing calls to servers should accept a Context.

The Context should be the first parameter, typically named ctx:

func (s *service) SendEmail(ctx context.Context, recipientAddress string, body string) error {
...
}

#----------------------------------------------
* Context

.image examples/images/context.PNG 345 775
#----------------------------------------------
* Context

Default context

ctx := context.Background()

Context with cancel

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Context with timeout

ctx, cancel := context.WithTimeout(context.Background(), 5 * time.Second)
defer cancel()

Context with assigned value

ip := "192.0.2.1"
ctx := context.WithValue(context.Background(), "userIP", ip)
ctx.Value("userIP")

#----------------------------------------------
* Generics
#----------------------------------------------
Expand Down

0 comments on commit b4c6b05

Please sign in to comment.