A global variable has a plausible chance to race as one goroutine reads it while another writes it, as shown in previous post: https://groups.google.com/g/golang-nuts/c/PHw_zU6Ayfo
So I am trying to enforce a lock on each global variable to avoid such accident just as Rust does on every variable. Here is one implementation where 'S' is the global variable. You can create Int, String, Bool, ArrayInt, ArrayStr, etc in a special package, and then call its type and function/methods from anywhere. I am not sure if map type needs such special care since I heard map is okay as one goroutine reads it while another writes it at the same time. package main import ( "fmt" "sync" ) type Int struct{ mutex sync.Mutex value int } func (I *Int) Save(n int){ I.mutex.Lock() I.value = n I.mutex.Unlock() } func (I *Int) Load() int{ I.mutex.Lock() defer I.mutex.Unlock() return I.value } var S = &Int{} func main() { S.Save(5) fmt.Printf("S=%v, S.value=%d \n", S, S.value) fmt.Println("S:", S.Load()) } Any advice? -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/967069a9-8ba8-4d20-b02b-0399e31f3fffn%40googlegroups.com.