-3

When I use go mod and I have just one .go file all things are ok and go mod can download the external package and use it but when I use the external package in the other file (not main.go file) I get this error (when run go run main.go)

test/test.go:4:2: cannot find package

My project structure is like this:

├── go.mod
├── go.sum
├── main.go
└── test
    └── test.go

And that is my files:

main.go

package main

import (
    "./test"
)

func main() {
    test.Hello()
}

test.go

package test

import (
    "github.com/mehrdadep/tgomod"
)

func Hello() {
    tgomod.Print()
}

go.mod

module test

go 1.15

require github.com/mehrdadep/tgomod v1.0.1

go.sum

github.com/mehrdadep/tgomod v1.0.1 h1:4lxx7JE0pySHLbH52sidkkKBjJQFC8ZZej3zEX/RTWc=
github.com/mehrdadep/tgomod v1.0.1/go.mod h1:YIkzdF7Sf9nd+eC0ySxL+gGbsew7LvUh9vP3p7yzTi4=

Thanks

azibom
  • 1,769
  • 1
  • 7
  • 21
  • I suggest you read "How to Write Go Code", since it is the official documentation which describes how to do this correctly. – JimB Dec 09 '20 at 20:39

1 Answers1

-1

I change the

import (
    "./test"
)

to

import (
    "test/test"
)

And it works
So what I found was that your path should be base on your module name and then add your directory to it
In test/test my first test is my module name and the second one is my directory name

azibom
  • 1,769
  • 1
  • 7
  • 21