panjf2000 / gnet

🚀 gnet is a high-performance, lightweight, non-blocking, event-driven networking framework written in pure Go.
https://gnet.host
Apache License 2.0
9.7k stars 1.04k forks source link

[Question]: 代码使用问题请教! #454

Closed maxbad closed 1 year ago

maxbad commented 1 year ago

Actions I've taken before I'm here

Questions with details

这是示例代码:


type SimpleCodec struct {
    Test int
}

func (codec SimpleCodec) Add() {
    codec.Test++
}

func (s *simpleServer) OnOpen(c gnet.Conn) (out []byte, action gnet.Action) {
    codec := new(SimpleCodec)
    logging.Infof("codec1:%p", &codec)
    codec.Test = 0
    c.SetContext(codec)
    return
}

func (s *simpleServer) OnTraffic(c gnet.Conn) (action gnet.Action) {
    codec := c.Context().(*SimpleCodec)
    logging.Infof("codec2:%p", &codec)
    codec.Test++
      // codec.Add()
    logging.Infof("Test:%d", codec.Test)
    return
}

这样是预期的正常输出,但是为什么 codec2打印的地址每次都不一样呢 2023-04-14T17:20:37.776284688+08:00 INFO logging/logger.go:182 codec1:0xc000196000 2023-04-14T17:20:38.886586256+08:00 INFO logging/logger.go:182 codec2:0xc000196008 2023-04-14T17:20:38.887074661+08:00 INFO logging/logger.go:182 Test:1 2023-04-14T17:20:39.582097075+08:00 INFO logging/logger.go:182 codec2:0xc000196010 2023-04-14T17:20:39.582207676+08:00 INFO logging/logger.go:182 Test:2 2023-04-14T17:20:40.177779716+08:00 INFO logging/logger.go:182 codec2:0xc000196018 2023-04-14T17:20:40.17823942+08:00 INFO logging/logger.go:182 Test:3 2023-04-14T17:20:40.519839226+08:00 INFO logging/logger.go:182 codec2:0xc000196020 2023-04-14T17:20:40.52037823+08:00 INFO logging/logger.go:182 Test:4 2023-04-14T17:20:40.823732499+08:00 INFO logging/logger.go:182 codec2:0xc000196028 2023-04-14T17:20:40.824186403+08:00 INFO logging/logger.go:182 Test:5 2023-04-14T17:20:41.283534644+08:00 INFO logging/logger.go:182 codec2:0xc000196030 2023-04-14T17:20:41.283983448+08:00 INFO logging/logger.go:182 Test:6

我把OnTraffic 的 codec.Test++ 改成 codec.Add(),打印结果就不是想要的结果了,这是为什么啊 2023-04-14T17:23:08.632478691+08:00 INFO logging/logger.go:182 codec1:0xc000196000 2023-04-14T17:23:09.606985175+08:00 INFO logging/logger.go:182 codec2:0xc000196008 2023-04-14T17:23:09.607471279+08:00 INFO logging/logger.go:182 Test:0 2023-04-14T17:23:10.314066003+08:00 INFO logging/logger.go:182 codec2:0xc000196010 2023-04-14T17:23:10.314471606+08:00 INFO logging/logger.go:182 Test:0 2023-04-14T17:23:10.709037581+08:00 INFO logging/logger.go:182 codec2:0xc000196018 2023-04-14T17:23:10.709484785+08:00 INFO logging/logger.go:182 Test:0 2023-04-14T17:23:11.106265378+08:00 INFO logging/logger.go:182 codec2:0xc000196020 2023-04-14T17:23:11.106718582+08:00 INFO logging/logger.go:182 Test:0 2023-04-14T17:23:11.555813535+08:00 INFO logging/logger.go:182 codec2:0xc000196028 2023-04-14T17:23:11.556325539+08:00 INFO logging/logger.go:182 Test:0 2023-04-14T17:23:12.027587587+08:00 INFO logging/logger.go:182 codec2:0xc000196030 2023-04-14T17:23:12.028022791+08:00 INFO logging/logger.go:182 Test:0

Code snippets (optional)

望大佬不吝赐教
panjf2000 commented 1 year ago

这样是预期的正常输出,但是为什么 codec2打印的地址每次都不一样呢

你的代码打印的是 codec 这个变量的地址,而非你的 SimpleCodec 的地址,所以每次打印的地址都不同,你需要改成:

logging.Infof("codec1:%p", codec)
logging.Infof("codec2:%p", codec)

我把OnTraffic 的 codec.Test++ 改成 codec.Add(),打印结果就不是想要的结果了,这是为什么啊

如果需要修改 struct 的状态,方法需要使用指针 receiver:

func (codec *SimpleCodec) Add() {
    codec.Test++
}

参考:https://github.com/golang/go/wiki/CodeReviewComments#receiver-type

看起来你是初学 Go 吧,我建议你还是先花点时间把 Golang 的基础夯实一下再来玩开源框架,特别是像 gnet 这样偏底层的网络框架