无法导入位于同一目录中的软件包

I want to import in the current file or package other file located in the same project in a directory. I'm doing this:

import (

// "./dir1"
  "/Users/my_name/my_project/dir1"

)

None of them works

  1) Cloning into '/Users/my_name/go/src/github.com/github_username/github_project'...
  fatal: could not read Username for 'https://github.com': terminal prompts disabled


  2) package /Users/my_name/my_project/dir1: unrecognized import path "/Users/my_name/my_project/dir1" (import path does not begin with hostname)

How to import a directory located in the current project?

Rename dir1 to to the same name as the package inside that directory, then you can import it with:

import "./package1"

However doing this is not recommended, use GOPATH instead. If you really don't want to use GOPATH, you may want to use Modules in Go 1.11 (but it is still experimental).

Import paths are not directly paths. They are relative to the GOPATH (found by doing echo $GOPATH).

This implies that go is very opinionated about where you store your code as well. So you will have to move your code to $GOPATH/src/my_name/my_project. If you are hosting your code on something like github then move it to $GOPATH/src/github.com/my_github_name/my_project.

Then when you import your sub-packages:

import "github.com/my_github_name/my_project"

Notice that it is not an absolute path.

Make sure your project is in GOPATH's go/src folder(Recommended way). Then import like this

package logic

import (
    "project_name/folder_name"
)