Created
July 13, 2012 04:45
-
-
Save acsellers/3102766 to your computer and use it in GitHub Desktop.
Scopable Go structs
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" | |
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