Skip to content

Instantly share code, notes, and snippets.

@mehran-prs
Last active March 6, 2023 12:05
Show Gist options
  • Save mehran-prs/f847006e3240f974dc407912b7213a52 to your computer and use it in GitHub Desktop.
Save mehran-prs/f847006e3240f974dc407912b7213a52 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"errors"
"log"
"time")
var totalInventory int
func AddToInventory(_ context.Context,inventory int) {
if err:=process(inventory);err!=nil{
log.Print("Inventory is not valid")
}
}
func process(inventory int) error{
if inventory<0{
return errors.New("inventory could not be negative")
}
// It takes two seconds to process the request.
time.Sleep(time.Second * 2)
log.Print("inventory proceed successfully")
totalInventory += inventory
return nil
}
func main() {
ctx:=context.Background()
addInventories(ctx)
}
func addInventories(ctx context.Context){
for i:=0;i<10;i++{
AddToInventory(ctx,i)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment