我正在嘗試編寫一個函數,但這里的問題讓我感到驚訝。userGroup.Use( middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) { if username == "joe" && password == "123"{ return true, nil } return false, nil }) // <- error happens here )userGroup.Use( middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) { if username == "joe" && password == "123"{ return true, nil } return false, nil })) // <- !!花了一個小時來解決一個錯誤,但結果是最后一個右括號不能隨意浮動。這是分號、逗號或縮進的問題嗎? 我記得 JS 不關心這種東西我得到的錯誤是:missing ',' before newline in argument list |syntax
1 回答

大話西游666
TA貢獻1817條經驗 獲得超14個贊
這是 Golang 分號規則的結果:https ://go.dev/doc/effective_go#semicolons ,其中 Go 在掃描源代碼時添加了一個分號,因此原始源代碼沒有分號。
遵循“如果換行符出現在可以結束語句的標記之后,插入分號”的規則,您之前的代碼如下所示:
userGroup.Use(
middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
if username == "joe" && password == "123"{
return true, nil
}
return false, nil
}); // <- semicolon added here
)
這當然是錯誤的并導致錯誤。移動該行的右括號而不是修復該問題。
- 1 回答
- 0 關注
- 93 瀏覽
添加回答
舉報
0/150
提交
取消