垃圾收集是否在使用之前存储在包装级变量中?
我有一个配置对象,该对象根据启动时的环境变量初始化了一些变量:
// init the conf object on startup and fail quickly if there's an environment issue
_ = utils.GetConf()
这是在我的服务器的init()方法中,在我的代码中的其他位置,我只调用getConf()获取配置对象。我想通过单顿模式实现此配置对象:
import (
"fmt"
env "github.com/Netflix/go-env"
)
type Conf struct {
DBName string `env:"MONGO_INITDB_DATABASE,required=true"`
Hostname string `env:"HOSTNAME,required=true"`
DBUsername string `env:"MONGO_INITDB_ROOT_USERNAME,required=true"`
DBPassword string `env:"MONGO_INITDB_ROOT_PASSWORD,required=true"`
}
var gConf *Conf
func GetConf() *Conf {
if gConf != nil {
return gConf
}
gConf = new(Conf)
_, err := env.UnmarshalFromEnviron(gConf)
if err != nil {
panic(err)
}
return gConf
}
由于我一开始就没有用utils.getConf()的返回值进行任何操作,所以Golang可能会垃圾收集我的配置对象和我的GCONF Pointer最终会指向无处吗?
I have a config object which initializes some variables based on environment variables at startup:
// init the conf object on startup and fail quickly if there's an environment issue
_ = utils.GetConf()
This is in the init() method of my server, and in other places in my code, I just call GetConf() to get the config object. I'd like to implement this config object through a singleton pattern:
import (
"fmt"
env "github.com/Netflix/go-env"
)
type Conf struct {
DBName string `env:"MONGO_INITDB_DATABASE,required=true"`
Hostname string `env:"HOSTNAME,required=true"`
DBUsername string `env:"MONGO_INITDB_ROOT_USERNAME,required=true"`
DBPassword string `env:"MONGO_INITDB_ROOT_PASSWORD,required=true"`
}
var gConf *Conf
func GetConf() *Conf {
if gConf != nil {
return gConf
}
gConf = new(Conf)
_, err := env.UnmarshalFromEnviron(gConf)
if err != nil {
panic(err)
}
return gConf
}
Since I'm not doing anything with the return value of utils.GetConf() at the start, is it possible that golang will garbage collect my config object and my gConf pointer will end up pointing to nowhere?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您创建一个指向值的指针,则指示指针将产生该值。
这实际上是您必须牢记的。垃圾收集系统可能会在现场做任何事情,但是上述语句必须仍然正确。如果垃圾收集器在使用您的指针之前将其无效,那将是该语言的破裂实现(即编译器错误)。
如果对GO的工作方式有疑问,请始终咨询 go语言规范。您会看到指针行为明确定义了。另一方面,垃圾收集仅在引言中提到而没有定义任何行为。因此,我们可以假设垃圾收集不应覆盖或以其他方式干扰Pointer应该使用的方式。
If you create a pointer to a value, dereferencing that pointer will yield that value.
That is really all you must bear in mind. The garbage collection system may be doing whatever it's doing behind the scene, but the above statement must still remain true. If the garbage collector were to invalidate your pointers before you use them, that would be a broken implementation of the language (i.e., a compiler bug).
When in doubt about how Go works, always consult the Go language specification. You'll see that pointer behaviour is plainly defined. Garbage collection, on the other hand, is only mentioned in the introduction without defining any behaviour. Therefore, we can assume that garbage collection should not override or otherwise interfere with the way pointers are supposed to work.