Last active
March 6, 2023 12:05
-
-
Save mehran-prs/f847006e3240f974dc407912b7213a52 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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