Search code examples
gogopathgo-packages

Cannot find nested package


I have a project with the following structure:

myapp/
  -services/
     -services/
         -exch.go
     -services.go
  -server.go

Having $GOPATH set to /home/dev/dev/go this is how server.go names it's package and imports:

//server.go
package main

import (
  "net/http"
  "github.com/labstack/echo"
  "myapp/services"
)

this is services.go:

//services.go
package services

import (
  "fmt"
  "myapp/services/exch"
)

and this is exch.go:

//exch.go
package exch

import (
  "net/http"
  "fmt"
  "io/ioutil"
  "encoding/json
)

Now, server.go imports package services fine, but services.go cannot find package exch. I tried changing the imports path several ways but cannot make it work. Am I missing something?

Might be useful to know that /myapp is located here: /home/dev/dev/go/src


Solution

  • One directory per package, one package per directory. If exch.go is supposed to be imported as services/exch, it needs to be in a directory services/exch, not in directory services/services.