Last active
November 28, 2017 11:41
-
-
Save arxdsilva/088a35bede7f4e5b9c44595c01be6491 to your computer and use it in GitHub Desktop.
Golang concurrency using method example and pizza orders
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 ( | |
"fmt" | |
"sync" | |
) | |
type PizzaOrder struct { | |
orderNum int | |
flavor string | |
} | |
func main() { | |
var wg sync.WaitGroup | |
orders := []*PizzaOrder{ | |
{orderNum: 0}, {orderNum: 1}, {orderNum: 2}, | |
{orderNum: 3}, {orderNum: 4}, {orderNum: 5}, | |
} | |
wg.Add(len(orders)) | |
for _, o := range orders { | |
go func(p *PizzaOrder) { | |
defer wg.Done() | |
p.makePizza() | |
}(o) | |
} | |
wg.Wait() | |
for _, order := range orders { | |
fmt.Printf("%v\n", *order) | |
} | |
} | |
func (p *PizzaOrder) makePizza() { | |
name := []string{"pepperoni", "double cheese", "gourmet", "vegie", "margherita", "meat"} | |
p.flavor = name[p.orderNum] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment