GOLANG unexpected directory layout error

I got this error trying to compile some GO code today and the usual searches didn’t immediately turn up the answer to my issue. The error output made it seem that something was wrong with the path setup in my GO environment which lead me into a dead end of searches and attempted resolutions.

The actual problem was very simple. I was importing with a path relative to the project directory and GO no longer approves of that. All import paths should now be relative to the src directory within the directory given by the $GOPATH environment variable.

Bad, don’t do this:

package mypkg 

import (
  "../example2/otherpkg"
)

Good, do this:

package mypkg

import (
  "github.com/example-user/otherpkg"
)

The example given above assumes a standard GO install where $GOPATH points to a directory which contains:

$GOPATH
  - src
    - github.com
      - example-user
        - otherpkg
        - mypkg
  - bin
  ...

Leave a Reply

Your email address will not be published. Required fields are marked *