为什么这个zerolog hook是一个无效的赋值?

huangapple go评论87阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2022年2月24日 18:25:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/71250492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定