codec
codec

Reputation: 8816

Relative imports in Go

I have a go Project with the following directory structure

utils(pkg)
   | auth.go (has a function names test1)
controllers(pkg)
   | login.go (has a function names test2)

I am trying to access function test1 from login.go. Here is what I have done

import "../utils"

func test2(c *gin.Context) bool{
      utils.test1()
}

But I always get Unresolved reference test1. I am new to go . Can anyone help why I am getting this error?

Upvotes: 80

Views: 130154

Answers (8)

Nek
Nek

Reputation: 3115

I just hit hard an error with relative path not working. Probably some old code that do not work with recent versions of go...

For the context, here was my error:

main.go:13:2: "../../generated/go/src/protos" is relative, but relative import paths are not supported in module mode

And my directory structure:

.
├── Makefile
├── README.md
├── generated
│   └── go
│       └── src
│           └── protos
│               ├── helloworld.pb.go
│               └── helloworld_grpc.pb.go
└── src
    ├── protos
    │   └── helloworld.proto
    └── server
        └── main.go

I fixed it in 2 steps:

  1. Adding 2 go.mod files (1 in server folder, the other in generated folder)
  2. Linking one to the other in .mod file configuration, see bellow

Server module:

module nek.test/grpc-server

go 1.23.3

require (
    golang.org/x/net v0.29.0 // indirect
    golang.org/x/sys v0.25.0 // indirect
    golang.org/x/text v0.18.0 // indirect
    google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
    google.golang.org/grpc v1.68.0 // indirect
    google.golang.org/protobuf v1.34.2 // indirect
    nek.test/grpc-generated v0.0.0-00010101000000-000000000000 // indirect
)

replace nek.test/grpc-generated => ../../generated

Generated files module:

module nek.test/grpc-generated

go 1.23.3

So at the end I have the following directory structure:

.
├── Makefile
├── README.md
├── generated
│   ├── go
│   │   └── src
│   │       └── protos
│   │           ├── helloworld.pb.go
│   │           └── helloworld_grpc.pb.go
│   └── go.mod
└── src
    ├── protos
    │   └── helloworld.proto
    └── server
        ├── go.mod
        ├── go.sum
        ├── main.go
        └── pkg

This configuration works for me!

Upvotes: 0

vivek-alladi
vivek-alladi

Reputation: 31

Suppose this is the directory

-calculations --> add.go

-main.go

Create a go.mod file in the root directory and enter the following:

module calculator
go 1.14

Then in main.go

import (
  "calculator/calculations"
  "fmt"
)
func main() {
fmt.Println(calculations.Add(5,9))

}

In add.go

package calculations
func Add(int x, int y) int {
return x+y
}

Upvotes: 3

hdante
hdante

Reputation: 8020

Given this directory configuration:

.
├── controllers
│   └── login.go
├── main.go
└── utils
    └── auth.go

File main.go:

package main

import "./controllers"

func main() {
    controllers.Test2("Hello")
}

File controllers/login.go:

package controllers

import "../utils"

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

File utils/auth.go:

package utils

import . "fmt"

func Test1(msg string) {
    Println(msg)
}

Result works:

$ GO111MODULE=auto go build -o program main.go 
$ ./program 
Hello

So what you wanted to do works. The only difference is that I've used upper case function names, because it's required to export symbols.

Upvotes: 6

Thomas S.
Thomas S.

Reputation: 6345

Here is another example project structure with file contents required to import correctly:

test1/
   utils/
      texts.go
   main.go
   go.mod

with following contents:

go.mod:

module mycompany.com/mytest

go 1.15

utils/texts.go (to make a function visible from a different package, it needs to start with an uppercase letter):

package utils

func GetText() string {
  return "hello world"
}

main.go (only the full import name is supported, there is no shortcut to import from the same module easier):

package main

import (
  "fmt"
  "mycompany.com/mytest/test1/utils"
)

func main() {
  fmt.Println(utils.GetText())
}

Upvotes: 24

rodmanb
rodmanb

Reputation: 101

It's possible as of Go 1.16, although still not as straightforward as it could be, by editing the go.mod file to resolve the package name to a relative path:

if you have a hello and a greeting packages side by side (hello contains main.go):

<home>/
 |-- greetings/  
    |--go.mod       <- init this as infra/greetings
    |--greetings.go <- let's say that this package is called greetings
 |-- hello/
    |--go.mod   <- init this as whatever/hello
    |--main.go  <- import infra/greetings

then edit hello's go.mod file and get the package:

go mod edit -replace infra/greetings=../greetings
go get infra/greetings
go mod tidy

There's a full example in the official documentation but hey, this might change again in the next version of Go.

Upvotes: 2

user6169399
user6169399

Reputation:

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

Upvotes: 71

marni
marni

Reputation: 4360

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

Upvotes: 71

nnnn20430
nnnn20430

Reputation: 175

I think you can just crate a vendor directory next to your source file, which acts like a relative GOPATH, and then create a relative symlink, which links to the package you want to import inside the vendor directory, and then import the package as if the vendor directory is your $GOPATH/src/.

Upvotes: 1

Related Questions