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
Post a Comment