Subpackage relative import

I am new to Golang and I wanted to learn by trying to create a logger with custom print format, so I have the following file structure:

logging (module named github.com/NikosGour/logging
-go.mod
-main.go (for testing the library)
-src
--lib.go
--models
---model.go

main.go

package main

import (
	"fmt"

	log "github.com/NikosGour/logging/src"
)

func main() {
	fmt.Println("Kati allo")
	log.Nikos("testing")
}

src/lib.go

package logging

import (
	"fmt"

	"github.com/NikosGour/logging/src/models"
)

func Nikos(str models.My_str) {
	fmt.Println(str)
}

src/models/model.go

package models

type My_str string

This all works, but I wonder, is there a way of relativly importing the packages??

in src/lib.go I would like to be able to do

import (
	"fmt"

	"./models"
)

Because you’re using the go mod management system, it’s not supported and not recommended; Specifying the package path solves many problems. If you just want to debug references other packages locally, you can try go work or add " replace github.com/xxx/xxx => . /xxx " to go.mod

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.