1

I'm trying to create a clean architecture API in Go but I'm confused with package logic. Here's the structure of the project.

    main.go                <- package main
    ├── delivery/
    │   ├── endpoints/
    │   │   ├── users.go   <- package endpoints (I want to import ./repository here)
    │   │   └── home.go    <- package endpoints
    │   ├── router.go      <- package delivery
    │   ├── middleware.go  <- package delivery
    ├── repository/
    │   ├── database.go    <- package repository
    │   └── fetchUsers.go  <- package repository

In router.go file I can access and import endpoints like this import "./endpoints", but when I try to import repository package inside users.go or inside home.go I get an error. Is there a way to access repository from files that are in endpoints folder?

1
  • 1
    Do not use relative imports! Commented Jul 20, 2019 at 14:49

1 Answer 1

2

The only recommended/canonical way to do imports is with full paths. If your code lives in github.com/Uranus/myproject, then in endpoints/users.go you should import repository thus:

import "github.com/Uranus/myproject/repository"

Note that it doesn't actually have to be on github. Using modules, you can have any import path you like while developing locally, as long as it's the module name in your go.mod file.

Follow the examples from this official Go blog post and you'll know what to do.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.