Reputation: 11479
I'm writing a simple program in Go as an exercise to learn the language. The program is a game player: it exec.Command
s a text-based game, then communicates with it via StdinPipe
/StdoutPipe
. After some fooling around and reading a lot of online docs I managed to get the skeleton working -- the equivalent of Hello World, where I've established two-way communication and can handle errors like the program terminating.
Now I'm trying to write the actual game-playing AI code. Since my purpose is learning the language, I'm trying to be very careful with style -- I don't just want to write C (or some other language) in Go.
The obvious division of labor in the program (once all the setup is done) is into two parts. First, the program looks at the current state and decides what command should be issued to the game. Second, the program looks at the data returned and updates the state accordingly. (Yes, it's a simple game -- it waits for input and then responds, there are no timing issues.)
I'm not sure where this state information should go. Dumping it all into the global scope feels wrong, and making a massive singleton object seems even worse (and Go isn't particularly OO). In any case I don't want to have the functions pass and return 20+ variables.
General advice is fine, but I'm most interested in what is idiomatically appropriate for Go. On request I can share code but I don't think it would be helpful.
Upvotes: 5
Views: 4380
Reputation: 12300
I like using a package for this purpose.
trivial example:
package foo
func Incr() {
f.incr()
}
func Count() int {
return f.count()
}
type foo struct {
sync.RWMutex
count int
}
func (f *foo) incr() {
f.Lock()
f.count++
f.Unlock()
}
func (f *foo) count() int {
f.RLock()
defer f.RUnlock()
return f.count
}
var f = &foo{}
This package can be imported into any other package and maintain state. I added the sync.RWMutex to prevent any race conditions. This gives you complete control of how foo's state is accessed and is nicely contained.
Upvotes: 6
Reputation: 6425
Go does enable a style of OO programming.
Create a struct type for the game state. Pass a pointer to a value of this type around in your program or store the pointer in a package level variable if that sort of thing does not bother you. Implement methods on this type as needed. Consider putting it in its own package for greater encapsulation.
Upvotes: 2