Last active
July 18, 2018 15:34
-
-
Save probablytom/968de0c3587a8bcb98378c7b0446ff44 to your computer and use it in GitHub Desktop.
Self-Managing, configurable structs for Edinburgh's Golang Developer Meetup, July '18
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 SelfIncrementingStruct | |
import "SelfManagingStructs/TomTypes" | |
var ( | |
runAlready = false | |
CollectionSize = 5 | |
usageLimit = 3 | |
SMSCollection = make(chan TomTypes.SelfManagingStruct, CollectionSize) | |
) | |
// Initialise a new struct properly, with the StructIncrementer as its "manager" | |
func constructNewSMStruct() TomTypes.SelfManagingStruct { | |
return TomTypes.SelfManagingStruct{ | |
Manager: StructIncrementer, | |
Counter: 0, | |
Limit: usageLimit, | |
} | |
} | |
// The function that'll increment values in our struct. | |
func StructIncrementer (sms TomTypes.SelfManagingStruct) { // NOTE: this is a TomTypes.ReturnManager | |
sms.Counter++ | |
// Refresh our structs if they hit a usage limit! | |
if sms.Counter == sms.Limit { | |
sms = constructNewSMStruct() | |
} | |
// Pop it back on the stack. | |
SMSCollection <- sms | |
} | |
// Get a self-incrementing struct from our collection of structs. | |
func GetSMStruct() TomTypes.SelfManagingStruct { | |
// If we haven't ever populated the channel before, do that. | |
if !runAlready { | |
var c int | |
for c = 0; c < CollectionSize; c++ { | |
SMSCollection <- constructNewSMStruct() | |
} | |
runAlready = true | |
} | |
// Just take something from the channel! | |
return <-SMSCollection | |
} |
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 ( | |
"SelfManagingStructs/SelfIncrementingStruct" | |
"SelfManagingStructs/TomTypes" | |
"fmt" | |
"time" | |
) | |
func main() { | |
// Loop through our struct and print the counter value. We'll see them reset as they reach a usage limit. | |
var incStruct TomTypes.SelfManagingStruct | |
for { | |
incStruct = SelfIncrementingStruct.GetSMStruct() | |
fmt.Println(incStruct.Counter) | |
incStruct.ManageSelf() | |
time.Sleep(500 * time.Millisecond) | |
} | |
} |
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 TomTypes | |
type ReturnManager func(SelfManagingStruct) | |
type SelfManagingStruct struct { | |
Manager ReturnManager | |
Counter int | |
Limit int | |
} | |
func (sms SelfManagingStruct) ManageSelf() { | |
sms.Manager(sms) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment