Import packages by folder only

I am trying to create an API. This API uses a “store” with CRUD endpoints for each database table (or task). This means that there can be maybe 100 packages in folder “store”.

How do I import this into my main library?
By pointing to the folder only?
Or is there a better way to do this?

image

users.go

package users

func Getusers() string {
   return ("Hi from user")
}

main.go

import "store"

fmt.Println(Getposts)
fmt.Println(Getusers)

Error message I get

./main.go:16:14: undefined: Getpost
./main.go:17:14: undefined: Getuser

You have to say store.Getusers

Doesn’t they have to use users.Getusers, as users is the package name, store is it’s path…

And shouldn’t this even fail for recent versions of go, as there is no host in the imported path?

Got another error instead:

cannot find package “api_server/main/store” in any of:
/usr/local/go/src/api_server/main/store (from $GOROOT)
/Users/sibert/go/src/api_server/main/store (from $GOPATH)

This is the exact path: /usr/local/go/src/api_server/main/store

What am I doing wrong?

Folder name is “store” and I have tried with both package name “store” and “users”. And I have tried to import “store”, “main/store” and “api_server/main/store”. Get errors whatever.

I have go version go1.14.3 darwin/amd64. Does it helps to update?

Maybe “./store”?

I have no idea! I always use fully qualified imports, but I thought that relative imports should still work.

I only tried relative imports once during my starting with go around 1.4 or 1.5… though dropped them right after the first couple of minutes as they caused errors (or warnings?) once I also added external dependencies…

Though relative imports should indeed be with leading dots as far as I remember.

Please where exactly have you tried store and users?

Given the code and folder architecture from Your op, I’d say you need to import "./store" and then users.Getusers().

Though I have no clue about your cat and posts.go, if the have not package users at the top, compilation should even fail, as there is only one package allowed per folder.

Package names and their import pathes do not necessarily need to match.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.