记一次因为共享变量的犯错误

记一次因为共享变量的犯错误,第1张

记一次因为共享变量的犯错误

下面由golang教程栏目给大家记一次因为共享变量的犯错误,希望对需要的朋友有所帮助!

问题复现

在models/User.php

var UserModel = new (User)

控制器

    if models.UserModel.Token == "" {
        models.UserModel.Token = "hello world"
    }

因为 var UserModel = new (User) 只会new 一次,每个请求都共享的这个变量的。

导致以后这个 if 只会执行一次,还是只能乖乖的 new 一个新的指针。

乖乖的 new

    var UserModel = new (models.User)
    if UserModel.Token == "" {
        UserModel.Token = "hello world"
    }

结尾

花费几小时 debug 才找到问题的,犯了基础的错误。

以上就是记一次因为共享变量的犯错误的详细内容,

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/684673.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-20
下一篇 2022-04-20

发表评论

登录后才能评论

评论列表(0条)

保存