I have the following code https://play.golang.org/p/4mlXMhqpLJ:
package main
import (
"fmt"
)
type A struct {
Name string
}
func (a *A) method(name string) {
a.Name = name
}
func (a *A) name() string {
return a.Name
}
type intt interface {
method(string)
name() string
}
var interfacer = []intt{
&A{},
}
func ExportedMethod(name string) bool {
for _, aa := range interfacer {
fmt.Println("Pre:", aa.name())
aa.method(name)
fmt.Println("Post:", aa.name())
if aa.name() == "Fred" {
return true
}
}
return false
}
func main() {
ExportedMethod("Bob")
ExportedMethod("Frank")
}
Since my second for loop prints Bob it is apparent that this is not safe for concurrent use. This is a simple example. In reality, I have many more structs that are going to satisfy interfacer...&B{}, &C{}, &D{}, &E{}, etc...I need a way to be able to loop through all of the structs, which is why I have a slice of interfacer. How can I make this safe for concurrent use?