-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve example with concurrency support (#573)
- Loading branch information
Showing
3 changed files
with
82 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,37 @@ | ||
package godogs | ||
|
||
// Godogs available to eat. | ||
var Godogs int | ||
import ( | ||
"fmt" | ||
) | ||
|
||
// Godogs is an example behavior holder. | ||
type Godogs int | ||
|
||
// Add increments Godogs count. | ||
func (g *Godogs) Add(n int) { | ||
*g = *g + Godogs(n) | ||
} | ||
|
||
// Eat decrements Godogs count or fails if there is not enough available. | ||
func (g *Godogs) Eat(n int) error { | ||
ng := Godogs(n) | ||
|
||
if (g == nil && ng > 0) || ng > *g { | ||
return fmt.Errorf("you cannot eat %d godogs, there are %d available", n, g.Available()) | ||
} | ||
|
||
if ng > 0 { | ||
*g = *g - ng | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Available returns the number of currently available Godogs. | ||
func (g *Godogs) Available() int { | ||
if g == nil { | ||
return 0 | ||
} | ||
|
||
return int(*g) | ||
} |
This file contains 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
This file contains 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