Elements. Build native projects for any modern development platform, using the language(s) of your choice. Oxygene (Object Pascal), C#, Swift, Java, Go. | RemObjects Software

SimpleGoRoutineChannel

Language: Gold, Platform: Island, Category: macOS
https://github.com/remobjects/ElementsSamples/tree/master/Gold/Island/macOS/SimpleGoRoutineChannel

  • SimpleGoRoutineChannel
    • References
      • gc
      • Island
      • Go
      • rtl
    • Source Files
    • Other Files

Program.go

package SimpleGoRoutineChannel

import (
	"fmt"
	"time"
)

// Sample to show go routines and how to synchronize using channels. In this case we
// use the isFinished channel to notify when a go routine (doWork) finished.
func main() {
	// create the channel
	isFinished := make(chan bool, 1)

	// this is a go routine which is executed in a separate thread
	go doWork(isFinished)

	// wait until somebody write to the channel
	<-isFinished
	fmt.Println("Done!")
}

func doWork(isFinished chan bool) {
	fmt.Println("doing things...")
	time.Sleep(time.Second * 2)
	// at this point we send a value to the channel, so if there is any process waiting, it gets unblocked
	isFinished <- true
}