Skip to content

Instantly share code, notes, and snippets.

@unexpand
Created November 6, 2017 18:11
Show Gist options
  • Save unexpand/87b463ba84feead5e4359f50cb975862 to your computer and use it in GitHub Desktop.
Save unexpand/87b463ba84feead5e4359f50cb975862 to your computer and use it in GitHub Desktop.
A simple go worker example
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