Problem in understanding of package access


(Beginner) #1

Hi All,

I am a new bee in Golang and trying to understand the concepts of Go and the best way to understand the go is “start writing code”.

So I am working on a project in but i am facing problem with package.
Now the problem statement is I am using go modules(i.e i done have my project on GOPATH).I ran the go mod init github.com/{my-repo}/myproject

The project structure is

±-----myproject
|
|------main.go
|
|------internal
|
|----cfg.go

Now the Code of cfg.go is

package internal

import “fmt”

func Test(){
fmt.Println(“This is just for testing”)
}

The out put of go.mod is

module github.com/rajeshcjain/DBCleanUpJob

go 1.12

The main.go file looks like

package DBCleanUpJob

import “./internal”

func main() {
internal.Test()
}

Now when i run this code with "go build main.go " it throws

main.go:3:8: unknown import path “_/Users/rajeshjain/projects/RiversProjects/DBCleanUpJob/internal”: internal error: module loader did not resolve import

Can you please help me with this …why am i getting this exception.

Other then this I was reading few blogs and where i discovered that internal module in a project is used to restrict the access of the package till that project as go used to have only to access local and global…so to overcome that we use internal.And internal can be accessed by the packages which are higher in the hierarchy to the internal i.e a/b/c/internal/d/e/f then a/b/c will be able to access the internal package…but what is the case for the packages which exists at the same level of internal…will they be able to access it.

Other then this few people in the blogs suggested that we need to create the project in the GOPATH and then access the internal project…then it is confusing for me as if we have to do that then what is the need to module which are introduced in Go…its a failure of that.

Thanks in advance.
Regards


(Johan Dahl) #2

Hi. I would import github.com/rajeshcjain/DBCleanUpJob/internal
instead


(Beginner) #3

Hi,

But for that do i need to copy the code to that location??

If i have to copy it there then whats the point of using go mod.


(Johan Dahl) #4

Sorry I thought the code was in the folder internal which is a subfolder of DBCleanUpJob


(system) closed #5

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