filegopackagedirectory

Go Package consisting of several folders


Is it possible in Go to have a package consisting of several .go files from different folders?

I am trying to make a subfolder inside of the main folder, and compiler says that it cannot find definitions ("undefined" error). When all the files are in the same folder, compilation does not give errors.

Thanks.


Solution

  • No, this is not possible.

    If you want to use folders inside your go project you have to use several packages.

    NOT VALID

    myproject
    | -- main.go (package main)
    | -- routes.go (package main)
    + -- models
         | -- db.go (package main)
         | -- mymodel.go (package main)
    

    VALID

    myproject
    | -- main.go (package main)
    | -- routes.go (package main)
    + -- models
         | -- db.go (package models)
         | -- mymodel.go (package models)