所以,今天在我編寫代碼時,我發現使用名稱創建一個函數會init產生一個錯誤method init() not found,但是當我將它重命名為startup它時一切正常?!癷nit”這個詞是為 Go 中的某些內部操作保留的,還是我在這里遺漏了什么?
2 回答

大話西游666
TA貢獻1817條經驗 獲得超14個贊
您還可以查看使用initin時可能遇到的不同錯誤golang/test/init.go
// Verify that erroneous use of init is detected.
// Does not compile.
package main
import "runtime"
func init() {
}
func main() {
init() // ERROR "undefined.*init"
runtime.init() // ERROR "unexported.*runtime\.init"
var _ = init // ERROR "undefined.*init"
}
init本身由golang/cmd/gc/init.c:
現在在cmd/compile/internal/gc/init.go:
/*
* a function named init is a special case.
* it is called by the initialization before
* main is run. to make it unique within a
* package and also uncallable, the name,
* normally "pkg.init", is altered to "pkg.init·1".
*/
- 2 回答
- 0 關注
- 207 瀏覽
添加回答
舉報
0/150
提交
取消