Reputation: 4585
I'm trying to import a func from a subfolder.
I have a main package
package main
import (
...
)
func handleRequests() {
http.HandleFunc("/health", health)
...
}
func main() {
handleRequests()
}
And then I have a folder called health
in which I have a file called health.go
.
package health
import (
...
)
func health(writer http.ResponseWriter, _ *http.Request) {
...
}
What shall my import
look like and how do I call my health func?
Upvotes: 0
Views: 83
Reputation: 2642
At this point, your import statement doesn't mean anything, because it health
package has no exported function or variable. You should check out the scoping in Go from the language spec. From here
Maybe consider checking out go modules
, since it is now the suggested way to handle any go program that has more than a file.
The quick answer is,
your health.go
package health
import (
...
)
func Handler(writer http.ResponseWriter, _ *http.Request) {
...
}
your main.go
package main
import (
github.com/blablabla/yourproject/health
)
func handleRequests() {
http.HandleFunc("/health", health.Handler)
...
}
func main() {
handleRequests()
}
Upvotes: 3
Reputation: 111
You must name function starting from uppercase symbol ("Health" instead of "health").
For example: health (your case) is private declaration, and Health would be public.
The same principle with types and variables naming.
Upvotes: 1