Relative imports in Go

This is now different since the introduction of go modules, from go 1.11.

Thus, if you switch to go modules, and if your module is called "m", then the idiomatic way to do relative imports in your project tree would be to use: import "m/utils" and import "m/controllers" in places where you need to import those packages in your project. For details, see: https://github.com/golang/go/wiki/Modules#do-modules-work-with-relative-imports-like-import-subdir


GoLand users - by default these forms of imports appear as errors in the IDE. You need to enable Go Modules integration in settings

enter image description here


No there is no relative import in Go.
you should use the absolute path considering GOPATH:

The GOPATH environment variable specifies the location of your workspace. It is likely the only environment variable you'll need to set when developing Go code. To get started, create a workspace directory and set GOPATH accordingly. see: https://golang.org/doc/code.html#GOPATH

Import paths

An import path is a string that uniquely identifies a package. A package's import path corresponds to its location inside a workspace or in a remote repository (explained below).

The packages from the standard library are given short import paths such as "fmt" and "net/http". For your own packages, you must choose a base path that is unlikely to collide with future additions to the standard library or other external libraries.

If you keep your code in a source repository somewhere, then you should use the root of that source repository as your base path. For instance, if you have a GitHub account at github.com/user, that should be your base path.

Note that you don't need to publish your code to a remote repository before you can build it. It's just a good habit to organize your code as if you will publish it someday. In practice you can choose any arbitrary path name, as long as it is unique to the standard library and greater Go ecosystem.

Example:

This example assumes you have set GOPATH=/goworkdir in your OS environment.

File: goworkdir/src/project1/utils/auth.go

package utils

func Test1() string {
    return "Test1"
}

File: goworkdir/src/project1/controllers/login.go

package controllers

import "project1/utils"

func Test2() string {
    return utils.Test1()
}

File: goworkdir/src/project1/main.go

package main

import (
    "fmt"
    "project1/controllers"
)

func main() {
    fmt.Println(controllers.Test2())
}

Now if you go run main.go you should see output:

Test1

Tags:

Go