« Back to Index

[Golang Generator (Yield) like Python]

View original Gist on GitHub

Tags: #go #golang #generator #yield

Golang Generator (Yield) like Python.go

package main

import "fmt"

const max = 10000000

var r int

func process(x int) bool {
	r = x ^ x ^ x

	return x == max
}

func getNumber(process func(x int) bool) {
	for i := 0; i < max; i++ {
		if process(i) {
			break
		}
	}
}

func main() {
	getNumber(process)
	fmt.Println(r) // 9999999
}