登录
首页 >  Golang >  Go问答

Go中方法指针和全局变量初始化行为的正确性解析

来源:stackoverflow

时间:2024-03-24 21:15:35 124浏览 收藏

在 Go 中,方法指针和全局变量的初始化行为受语言规范的依赖分析机制影响。当初始化全局变量时,依赖项也会被初始化。因此,在定义 `bar` 变量时,它依赖于 `foo` 变量,导致 `foo` 在此时被初始化为 `nil`。即使在 `init()` 函数中对 `foo` 进行赋值,也无法更改 `bar.f` 方法指针指向的目标,因为 `bar` 已在 `init()` 函数执行之前初始化。

问题内容

有人可以向我解释一下这种行为吗?我无法理解为什么会发生这种情况(仍在学习 go)。我的具体问题在源代码中用 question 标记。

谢谢, 迈克尔

package main

// Define a simple type and functions on this type.
type Foo struct{}
var foo *Foo

func (f *Foo) function() {
    if f == nil {
        panic("Why is f nil?")
    }
}

// Create a wrapper struct containg method pointers to a global receiver variable.
type Example struct {
    f func()
}

var bar = &Example{
    // QUESTION: When is foo actually evaluated? It seems at definition of bar and then it's fixed? 
    // QUESTION: And why is its value at runtime not used to determine the (now initialized) receiver target?
    f: foo.function,
}

// Initialize the global variable.
func init() {
    foo = new(Foo)
}

// Call the function on foo, which should be initialized in init.
func main() {
    bar.f()
}

解决方案


这是语言规范中的相关部分:

https://golang.org/ref/spec#Package_initialization

所有全局变量都是根据依赖分析来初始化的。所以当它初始化时:

var bar = &example{
    f: foo.function
}

它需要初始化 foo。由于没有为 foo 定义初始值设定项,因此它为 nil。一旦所有变量初始化完成,init()函数就会运行并将foo设置为非零值。

如果将声明更改为:

var foo = new(Foo)

它有效。

本篇关于《Go中方法指针和全局变量初始化行为的正确性解析》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>