how do i import packages in go-lang or how to structure correctly package structure?

398 views Asked by At

I have following folder structure here

   server
       -main.go
     -cmd
         -config
           -config.go    
           -handlerr.go   
     -handlers
           -handlers.go         
     -pkg 
         -models       
           -models.go  
           -db       
             -db.go
     -router
           -router.go

and when I am trying to import the "models package" into db package it says "invalid import path:...",this structure i am following with book ,so what am i doing wrong ?How do i import the models into db functions or should i replace them (db and models),any suggest? enter image description here

2

There are 2 answers

1
PurplePika On

Here are the files in the question filled out. The import from db to models is illustrated. I set the module name to "my.example". Change that name to meet your needs. It's best to pick something including a "." to avoid conflict with a standard package. You can run this code on the Go PlayGround. How to Write Go Code explains all of this stuff in detail.

-- main.go --

package main

import (
    "my.example/pkg/models/db"
)

func main() {
    db.Hello()
}

-- go.mod --

module my.example

-- cmd/config/config.go --

package config

-- cmd/config/handlerr.go --

package config

-- handlers/handlers.go --

package handlers

-- pkg/models/models.go --

package models

func Hello() string { return "hello from models" }

-- pkg/models/db/db.go --

package db

import (
    "fmt"

    "my.example/pkg/models"
)

func Hello() {
    fmt.Println("hello from db and", models.Hello())
}

-- pkg/router/router.go --

package router

Regarding the layout of the files: This is in the land of opinions, so I will just ask you a question. Does the extra level of nesting in the pkg and cmd gain you anything?

1
y0u4r3w3 On

UPDATE: As @maxm reminded me, use GOPATH, here is a great example, that he mentioned.

So, if my web search and a little example program are correct, this is pretty much what is happening here.

Golang sees local packages as folders with .go files, on practice /modules/module.go if modules.go has package modules on the 1st line.

So, when we are including a local package, we are actually including a folder. I created a little example:

/project
      main.go
      /modules
          modules.go
          /printer
              printer.go

main.go

package main

import (
    "./modules/printer"
)

func main() {
    printer.Print()
}

modules/modules.go

package modules

import (
    "fmt"
)

func Modules() {
    fmt.Println("Modules Package")
}

modules/printer/printer.go

package printer

import (
    "../../modules"
)

func Print() {
    modules.Modules()
}

And this actually works! So, you can import a parent module, you just need to include the folder.

Of course, we cannot have import cycle, Golang won't allow it and if you are having this kind of error, it's better to change the structure of your program.

I'm not talking about paths and stuff, it's just a practical solution. So, smart people, please correct me!