如何从子目录中的控制器调用函数-Golang

I am trying to make a web app, without using a Framework like Revel and just using the Gorilla toolkit,

So far I have structured my app like this:

/App
- Controllers
   - Index.go
- Views
   - Index.html
- Public
   - css
   - js
   - img
- main.go

My main.go looks like:

package main

import (
    "github.com/gorilla/mux"
    "net/http"
)

func main() {

    r := mux.NewRouter()

    r.HandleFunc("/", Index)

    http.Handle("/", r)

    http.ListenAndServe(":8080", nil)
}

And Index.go looks like

package main

import (
    "fmt"
    "net/http"
)

func Index(res http.ResponseWriter, req *http.Request) {
    fmt.Println("Here")
}

But when I go run main.go it says Index is undefined, I'm not sure how to call functions from another file in a subdirectory. Any information would be great thanks.

Go convention prevents this. You must keep Index() within the main package.

Once you have some code that can be separated out, you will then create a sub-directory (package) and import it into main.

https://golang.org/doc/effective_go.html

Go code organization rules require one directory per package. Since your package main is split over the root directory and the controllers directory, your code organization won't work.

You can either make controllers a package and then import it into main, which I'd only recommend if you are projecting for a large codebase, or simply relocate the controllers file to the root directory.