3
package main

import (
    "fmt"
    "controller/userhandler"   //not able to import this custom package      
    "github.com/gin-gonic/gin"                                         
    "net/http"                                                         
    "github.com/jinzhu/gorm"                                               
  _ "github.com/jinzhu/gorm/dialects/postgres"
)

func main(){
}

The code below shows that Go is only searching for packages in GOROOT. After not finding controller/userhandler in GOROOT, ideally, it should look for packages in GOPATH but it does not.

I have already set GOPATH to my workspace path which includes the folders: bin, src, and pkg.

$ go build main.go                                                           
main.go:5:2: package controller/userhandler is not in GOROOT (/usr/local/go/src/controller/userhandler)
Fanan Dala
  • 584
  • 6
  • 19
  • can you please show your code? – namar sood Jul 06 '20 at 17:36
  • 2
    Go uses modules by default (see [How to Write Go Code](https://golang.org/doc/code.html), and the [Modules wiki page](https://github.com/golang/go/wiki/Modules)). Without a [mre] we can't tell exactly what you're doing. – JimB Jul 06 '20 at 17:40
  • Does this answer your question? https://stackoverflow.com/a/62573465/7715823 – Fanan Dala Jul 07 '20 at 08:37

1 Answers1

3

run go mod init MODULE_NAME (if the project is outside GOROOT or GOPATH) or just simply go mod init (if the project is inside GOROOT or GOPATH). The command should be run on the root folder of your project. This would create a go.mod file that would enable go to resolve your packages.

Fanan Dala
  • 584
  • 6
  • 19