I have a public variable which we call Tpus, I modify this value within an infinite for loop, however i get 2 different results when i try to print Tpus in the for loop, and in a goroutine outside the for loop, What im looking to do is get the same result in the goroutine as i get in the for loop.
var Tpus []string
func TPU_Calc(destination string) {
clusternodes := GetClusterNodes(destination)
go func() {
wg.Add(1)
time.Sleep(2 * time.Second)
for {
time.Sleep(150 * time.Millisecond)
//fmt.Printf("\n + %e", Tpus)
}
}()
for {
slot := GetSlot(destination)
slotleaders := GetSlotLeaders(destination, strconv.FormatUint(slot+10, 10))
for x := 0; x < 80; x++ {
for z := 0; z < len(clusternodes.Result); z++ {
if slotleaders[x] == clusternodes.Result[z].Pubkey {
if len(Tpus) >= 2 {
X := RemoveIndex(Tpus, 0)
Tpus = append(X, clusternodes.Result[x].Tpu)
fmt.Printf("\n + %e", Tpus)
} else {
Tpus = append(Tpus, clusternodes.Result[x].Tpu)
}
}
}
}
}
}
Result From Code Above:
var Tpus []string
func TPU_Calc(destination string) {
clusternodes := GetClusterNodes(destination)
go func() {
wg.Add(1)
time.Sleep(2 * time.Second)
for {
time.Sleep(150 * time.Millisecond)
fmt.Printf("\n + %e", Tpus)
}
}()
for {
slot := GetSlot(destination)
slotleaders := GetSlotLeaders(destination, strconv.FormatUint(slot+10, 10))
for x := 0; x < 80; x++ {
for z := 0; z < len(clusternodes.Result); z++ {
if slotleaders[x] == clusternodes.Result[z].Pubkey {
if len(Tpus) >= 2 {
X := RemoveIndex(Tpus, 0)
Tpus = append(X, clusternodes.Result[x].Tpu)
//fmt.Printf("\n + %e", Tpus)
} else {
Tpus = append(Tpus, clusternodes.Result[x].Tpu)
}
}
}
}
}
}
Result From Code Above:
var globalvar []string
func main() {
var mu sync.Mutex
go func() {
for {
// Block access to a global variable, so that
// no one can change it outside the goroutine.
// If it's already locked outside the goroutine,
// then wait for unlocking.
mu.Lock()
// Some actions with a global variable...
fmt.Printf("%v\n", globalvar)
// Unlocking access to a global variable
mu.Unlock()
// Some code...
}
}()
for i := 0; i < 255; i++ {
// Block access to a global variable.
// If it's already locked inside the goroutine,
// then wait for unlocking.
mu.Lock()
// Some actions with a global variable
globalvar = append(globalvar, "Str #"+strconv.Itoa(i))
// Unlock access
mu.Unlock()
// Some code...
}
}
Also you can define a special structure with mutex, value and methods for change it value. Something like this:
type TpusContainer struct {
mu sync.Mutex
value []string
}
func (t *TpusContainer) RemoveIndex(i int) {
t.mu.Lock()
defer t.mu.Unlock()
t.value = RemoveIndex(t.value, i)
}
func (t *TpusContainer) Append(elem string) {
t.mu.Lock()
defer t.mu.Unlock()
t.value = append(t.value, elem)
}
func (t *TpusContainer) String() string {
t.mu.Lock()
defer t.mu.Unlock()
return fmt.Sprintf("%v", t.value)
}
var Tpus TpusContainer
func main() {
go func() {
for {
fmt.Printf("%v\n", Tpus)
}
}()
for i := 0; i < 255; i++ {
Tpus.RemoveIndex(0)
Tpus.Append("Some string #"+strconv.Itoa(i))
}
}
Personally, I prefer the second approach.