Search code examples
gopackagesubdirectory

How to import all sub-directories from a certain app directory?


I am creating a http server with Golang. (I have a Php dev background.) In the internal/app/model I plan on making several packages with artificial logic and I would wish to load all the sub-directories of the model folder in main.go.

I have following folder structure:

[project folder]
\_cmd
     \_main.go
.
.
.
\_internal
          \_app
               \_model
                      \_article
                      \_product

I have tried something in this sense without success:

import (
    "[project_folder]/[project]/internal/app/model/*"
)

Solution

  • You would either:

    • need to place project_folder in $GOPATH/src (as in here): the old way
    • use a go mod init project_folder in order to declare a module named project_folder: the new way, with go modules. (use set GO111MODULE=on or export GO111MODULE=on)

    In the latter case, your project_folder can be anywhere: no more GOPATH.