go - When is the value outside a clusure in golang to be used? -


var loggers []*logoutput = nil  func init() {     loggers = make([]*logoutput, int(logger_usage_type_count)) }  func abc() {     loggers[loggertype].ticker = time.newticker(time.second *      time.duration(logrotatecheckinterval))     go func() {         defer recoverfunc()         {             <-loggers[loggertype].ticker.c             loggers[loggertype].checkandrotatelogfile()         }     }() } 

<-loggers[loggertype].ticker.c panic. , there's 1 goroutine modifying loggers[loggertype] , loggers[loggertype].ticker. found when panic happens, loggers[loggertype].ticker nil, makes me feel goroutine use value of loggers[loggertype].ticker before loggers[loggertype].ticker = time.newticker(time.second * time.duration(logrotatecheckinterval)) executed.

could explain when closure takes value of outside variable? change logger pointer variable goroutine func solution


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -