I have a folder structure cmd/web/main.go and handler.go, routes.go are also present in the same level as main.go, all declared as package main.
main.go
package main
type WebApp struct {
dbModel *models.UserModel
}
func main() {
db, err := dbSetUp()
if err != nil {
log.Fatal(err)
}
app := &WebApp{
dbModel: &models.UserModel{DB: db},
}
webServer := &http.Server{
Addr: ":5030",
Handler: app.Routes(),
IdleTimeout: time.Minute,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
fmt.Println("server running...")
err = webServer.ListenAndServe()
log.Fatal(err)
}
routes.go
package main
func (a *WebApp) Routes() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/signup", a.Signup)
return mux
}
go run cmd/web/main.go, compiler complains app.Routes undefiend (type *WebApp has no field or method Routes), but the go tools(formatter) does not complain such in the IDE.
If rearrange, putting the methods in the main.go file, all goes fine. When i research online, i found something similar suggesting it my improper use of go run command. what am i doing wrongly?. thanks.