Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function Go函数定义在另一个包中_Function_Go_Declaration - Fatal编程技术网

Function Go函数定义在另一个包中

Function Go函数定义在另一个包中,function,go,declaration,Function,Go,Declaration,我正在读一篇关于时间的帖子。startTimer声明和定义 根据答案,time.startTimer在src/time/sleep.go中声明 详情如下: func startTimer(*runtimeTimer) func startTimer(t *timer) { if raceenabled { racerelease(unsafe.Pointer(t)) } addtimer(t) } 其定义在src/runtime/time.go中,如下

我正在读一篇关于时间的帖子。startTimer声明和定义

根据答案,time.startTimer在
src/time/sleep.go中声明
详情如下:

func startTimer(*runtimeTimer)
func startTimer(t *timer) {
    if raceenabled {
        racerelease(unsafe.Pointer(t))
    }
    addtimer(t)
}
其定义在
src/runtime/time.go
中,如下所示:

func startTimer(*runtimeTimer)
func startTimer(t *timer) {
    if raceenabled {
        racerelease(unsafe.Pointer(t))
    }
    addtimer(t)
}
因此,您似乎可以在一个.go文件中声明一个函数,并在另一个.go文件中实现它。我尝试了同样的方法,例如,在a.go中声明一个函数并在b.go中实现它,但是当运行a.go时总是失败。这样做正确吗?如何声明在另一个.go文件中实现的函数?
sleep.go
time.go
中都没有
import
。你是怎么做的


谢谢

如果您查看
startTimer
正文上方的一行,您将看到go编译器的特殊指令:

//go:linkname stopTimer time.stopTimer

//go:linkname指令指示编译器使用“importpath.name”作为源代码中声明为“localname”的变量或函数的对象文件符号名。由于该指令可以破坏类型系统和包模块化,因此仅在导入“不安全”的文件中启用该指令


还请注意,用户代码中不应依赖所有pragma,“Pragmas不是语言的一部分。它们可能由gc编译器实现,但在规范中找不到。”该指令不适用于Pragmas。这里有一个很好的解释:简单:停止执行
go run
并使用适当的
go build