英文:
Why is this zerolog hook an ineffective assignment?
问题
我想将zerolog
的消息存储在一个切片中,以便轻松访问最后的n条消息。memoryLog.messages
保持为null,并且golangci-lint
报告了ineffective assignment to field memoryLog.messages
的问题。我对Go还很陌生,感觉好像漏掉了一些明显的东西,但我就是找不出来。
type memoryLog struct {
config config
messages []string
}
func (memoryLog memoryLog) Run(event *zerolog.Event, level zerolog.Level, message string) {
memoryLog.messages = append([]string{message}, memoryLog.messages...)
if len(memoryLog.messages) > memoryLog.config.MemoryLogMaxItems {
memoryLog.messages = memoryLog.messages[:memoryLog.config.MemoryLogMaxItems]
}
}
memoryLog := memoryLog{config: config}
logger := log.Hook(memoryLog)
英文:
I want to store zerolog
messages in a slice for easy access to the last n messages. memoryLog.messages
stays null and golangci-lint
complains about ineffective assignment to field memoryLog.messages
. I'm still new to Go and it feels like I'm missing something obvious, but I just can't figure it out.
type memoryLog struct {
config config
messages []string
}
func (memoryLog memoryLog) Run(event *zerolog.Event, level zerolog.Level, message string) {
memoryLog.messages = append([]string{message}, memoryLog.messages...)
if len(memoryLog.messages) > memoryLog.config.MemoryLogMaxItems {
memoryLog.messages = memoryLog.messages[:memoryLog.config.MemoryLogMaxItems]
}
}
memoryLog := memoryLog{config: config}
logger := log.Hook(memoryLog)
答案1
得分: 1
感谢我得到的帮助,它正在工作:
func (log *memoryLog) Run(event *zerolog.Event, level zerolog.Level, message string) {
//...
}
logger := log.Hook(&memoryLog)
英文:
Thanks to the help I got it working:
func (log *memoryLog) Run(event *zerolog.Event, level zerolog.Level, message string) {
//...
}
logger := log.Hook(&memoryLog)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论