So I was looking at the file here.
They call record := &accessLog
but they don't ever Initialize it as a variable first and if they do it that way if there are multiple simultaneous connections is there a possibility record will get over written with somebody else's data?
type accessLog struct {
ip, method, uri, protocol, host string
elapsedTime time.Duration
}
func LogAccess(w http.ResponseWriter, req *http.Request, duration time.Duration) {
clientIP := req.RemoteAddr
if colon := strings.LastIndex(clientIP, ":"); colon != -1 {
clientIP = clientIP[:colon]
}
record := &accessLog{
ip: clientIP,
method: req.Method,
uri: req.RequestURI,
protocol: req.Proto,
host: req.Host,
elapsedTime: duration,
}
writeAccessLog(record)
}
Go is a garbage collected language. The struct the pointer is pointing to will be valid as long as there's a reference to it. Multiple connections have nothing to do with this, as this creates a new record
every time LogAccess
is called, and if you follow the code in question you'll see that the reference lives at least to the end of writeAccessLog
, possibly longer depending on the implementation of glog.Infoln
.
To be clear &someType { ... fields ...}
creates a new (unnamed) instance of someType
, and then returns the address of that instance.