Created
November 6, 2017 18:11
-
-
Save unexpand/87b463ba84feead5e4359f50cb975862 to your computer and use it in GitHub Desktop.
A simple go worker example
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
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
type Result struct { | |
Name string | |
Id int | |
} | |
func Worker(jobs <- chan Result){ | |
select { | |
case msg := <-jobs: | |
fmt.Println("received message", msg.Name) | |
default: | |
fmt.Println("no activity") | |
} | |
} | |
func main() { | |
jobs := make(chan Result, 100) | |
res := new(Result) | |
res.Name = "John" | |
res.Id = 1 | |
jobs <- *res | |
go Worker(jobs) | |
fmt.Println("on ... 1") | |
res1 := new(Result) | |
res1.Name = "Sean" | |
res1.Id = 2 | |
jobs <- *res1 | |
go Worker(jobs) | |
fmt.Println("on ... 2") | |
time.Sleep(10 * time.Second) | |
close(jobs) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment