Skip to content

Instantly share code, notes, and snippets.

@acsellers
Created July 13, 2012 04:45
Show Gist options
  • Save acsellers/3102766 to your computer and use it in GitHub Desktop.
Save acsellers/3102766 to your computer and use it in GitHub Desktop.
Scopable Go structs
package main
import "fmt"
type Scope struct {
Minimum, Maximum, Current int
Transformer func(inst *int)
}
func (s *Scope)Find(min int, interval int){
s.Current, s.Minimum = min,min
s.updateTransform(interval)
}
func (s *Scope)updateTransform(stretch int){
s.Transformer = func(inst *int){
*inst = s.Current
s.Current += stretch
}
}
func (s *Scope)Count() int {
return s.Maximum - s.Minimum + 1
}
func (s *Scope)Transform(inst *int){
s.Transformer(inst)
}
func main() {
s := new(Scope)
s.Find(5,2)
s.Maximum = 10
a := make([]int, s.Count())
for i,_ := range a {
s.Transform(&a[i])
}
fmt.Println(a)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment