0

This import worked fine when I was starting app with dev_appserver.py

I am trying to refactor to use Firestore and Go 1.13

app.go content

package main
import (
    "net/http"
    "workout"
)

Project structure:

app.go  
|-- workout dir  
    |-- workout.go file that contains (package workout)  

From the root of the Working Directory I ran:
$ go run *.go

app.go:15:2: cannot find package "workout" in any of:
    /usr/local/go/src/workout (from $GOROOT)
    /Users/X/go/src/workout (from $GOPATH)
$ go run *.go workout/*.go
named files must all be in one directory; have ./ and workout/
X@MacBook-Pro Thu Oct 31 10:48:13 ~/Dropbox/go/src/workoutNew 
$ go build   
app.go:15:2: cannot find package "workout" in any of:
    /usr/local/go/src/workout (from $GOROOT)
    /Users/X/go/src/workout (from $GOPATH)
GolangNewb
  • 125
  • 2
  • 8
  • The errors show that the import path is expected to be `/Users/X/go/src/workout`, but it's not there. Are you trying to use modules? – JimB Oct 31 '19 at 19:24
  • I'm not trying to use Modules. I'm trying to import a package from a subdirectory of the Working Directory. – GolangNewb Oct 31 '19 at 21:18
  • 1
    The documentation explains how GOPATH imports work: try [How to Write Go Code](https://golang.org/doc/code.html) – JimB Oct 31 '19 at 21:22
  • So if I create a new project all the files have to be in one directory and all packages I create have to be located in ~/go/src or another location on $GOPATH ? – GolangNewb Nov 01 '19 at 15:09
  • If you're using `GOPATH`, _all_ packages must be relative to `GOAPTH`, period. Walking through How to Write Go Code provides a complete example of this. If you want to use modules, you need to first create a module and then you can import your module packages by their full module path. https://github.com/golang/go/wiki/Modules – JimB Nov 01 '19 at 15:54
  • The project I'm posting about is @ $GOPATH/src/AppDir and the import $GOPATH/src/AppDir/workoutDir/workout.go which is generating the error. – GolangNewb Nov 01 '19 at 19:21
  • well, following the documentation the import path for `workout` is `AppDir/workoutDir` (BTW, do _not_ use mixed case import paths, you cannot treat all systems as case-insensitive and it's likely to cause problems) – JimB Nov 01 '19 at 19:24

2 Answers2

6
import (
 "workout"
)

This will try to import a stdlib package called workout.

In order to import your workout package, you should name your main package (using go mod init), like: github.com/me/myapp, then import the workout package as github.com/me/myapp/workout.

Importing workout as a relative directory ( "./workout") also works, but that is not the recommended way of doing it.

Burak Serdar
  • 46,455
  • 3
  • 40
  • 59
0

Your GOPATH and GOROOT:

/usr/local/go/src/workout (from $GOROOT)
/Users/X/go/src/workout (from $GOPATH)

Move your working folder under:

$GOPATH/my-app/
my-app
|...app.go
    workout
    |...workout.go

Update import:

package main
import (
    "net/http"
    "my-app/workout"
)

Now move to:

cd $GOPATH/my-app/

Run app.go:

go run app.go
Kamol Hasan
  • 12,218
  • 1
  • 37
  • 46
  • I thought the convention was to put package files in a subdirectory that shared the same name as the package. I am running app.go from the Working Directory. – GolangNewb Oct 31 '19 at 21:13