Golang Module problem--package xxx/xxxx is not in GOROOT

3.6k views Asked by At

so here is what my directory is:

go
|-src
   |-ppppppSample
        |-newFolderOne
            |-firstSample.go
        |-hello.go
        |-go.mod

and here is the content of hello.go

package main

import (
    "fmt"
    jjj "ppppppSample/newFolderOne"
)



func main() {
    fmt.Println("start to test")
    fmt.Println(jjj.FirstVVVV)
}

here is the content of firstSample.go

package newFolderOne

var FirstVVVV = "Im first SSSSSSSSSSSS"

and here is my go.mod's content

module mmmmmppppp

go 1.15

when giving it the cmd go run hello.go, the terminal came out with like this:

D:\Users\eien_zheng\go\src\ppppppSample>go run hello.go hello.go:5:2: package ppppppSample/newFolderOne is not in GOROOT (C:\Go\src\ppppppSample\newFolderOne)

So here is my question:

(since I'm new for golang, I wish you guys could understand and tolerate some of my misunderstanding)

According to my understanding to Go module (maybe it's wrong), the Go module's function is gonna let some kind of online resource be downloaded to the directory GOPATH/pkg/mod instead of existing in GOROOT. No matter which directory your project in, your project can still import those resource from GOPATH/pkg/mod if you init Go module. But!!, in my understanding, it still can use package system to import package around project directory, in the meantime import online resource by Go module system.

How is that when I do (mod init) for hello.go, then it loses the (basic package import function) for this project?

2

There are 2 answers

1
Vikashsurin On BEST ANSWER
|--src
    |--sample
         |--newFolder
            |-firstSample.go       (package xyz)
         |--hello.go               (package main  import(xyz "sample/newFolder")
         |--go mod                 (module sample go 1.15)        

go mod should reference the root folder , here the root folder is |--sample

module sample go v1.xx

inside hello.go;

  package main
  import ( xyz "sample/newFolder")

and make sure exported functins or variables use camelCase aka starts with BlockLetters.
0
shant On

Import packages within a module using the module's path:

package main

import (
    "fmt"
    jjj "mmmmmppppp/newFolderOne"
)

...

Run it on the Playground.