亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么你不能在 Go 中命名一個函數“init”?

為什么你不能在 Go 中命名一個函數“init”?

Go
哈士奇WWW 2021-08-16 15:55:26
所以,今天在我編寫代碼時,我發現使用名稱創建一個函數會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".

*/


查看完整回答
反對 回復 2021-08-16
  • 2 回答
  • 0 關注
  • 207 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號