export 與 import
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
使用以下方法即可 import 另一個資料夾的 func,與go mod 不同的是 import時需要多寫上路徑。
新增一個資料夾與一個要export的檔案
package concurrency
import (
"fmt"
)
func Say(s string) {
fmt.Print(s)
}
記得export 的 function 第一個字要大寫
2. 新增 要 import的檔案
hello.go
package main;
import (
concurrency "./concurrency"
)
func main() {
concurrency.Say("hello");
}
之後即可。
go mod init test-backend
例如在 route 檔案可使用如下:
package routes
import (
controllers "test-backend/controllers"
)
func CatchphrasesRoute(route fiber.Router) {
route.Get("/", controllers.GetAllCatchphrases)
}