登录
首页 >  Golang >  Go问答

Golang模块问题--package xxx/xxxx不在GOROOT中

来源:stackoverflow

时间:2024-04-04 13:15:32 308浏览 收藏

学习知识要善于思考,思考,再思考!今天golang学习网小编就给大家带来《Golang模块问题--package xxx/xxxx不在GOROOT中》,以下内容主要包含等知识点,如果你正在学习或准备学习Golang,就都不要错过本文啦~让我们一起来看看吧,能帮助到你就更好了!

问题内容

所以这是我的目录:

go
|-src
   |-ppppppsample
        |-newfolderone
            |-firstsample.go
        |-hello.go
        |-go.mod

这是 hello.go 的内容

package main

import (
    "fmt"
    jjj "ppppppsample/newfolderone"
)



func main() {
    fmt.println("start to test")
    fmt.println(jjj.firstvvvv)
}

这里是firstsample.go的内容

package newfolderone

var firstvvvv = "im first ssssssssssss"

这是我的 go.mod 的内容

module mmmmmppppp

go 1.15

当给它命令 go run hello.go 时,终端显示如下:

d:\users\eien_zheng\go\src\ppppppsample>go 运行 hello.go hello.go:5:2: 包 ppppppsample/newfolderone 不在 goroot 中 (c:\go\src\ppppppsample\newfolderone)

这是我的问题:

(由于我是golang新手,希望大家能够理解并包容我的一些误解)

根据我对go模块的理解(也许是错误的),go模块的功能是让某种在线资源下载到gopath/pkg/mod目录中,而不是存在于goroot中。 无论您的项目位于哪个目录,如果您初始化 go 模块,您的项目仍然可以从 gopath/pkg/mod 导入这些资源。 但是!!,在我的理解中,它仍然可以使用包系统在项目目录周围导入包,同时通过go模块系统导入在线资源。

当我对 hello.go 执行 (mod init) 时,它会丢失该项目的(基本包导入功能)吗?


解决方案


|--src
    |--sample
         |--newfolder
            |-firstsample.go       (package xyz)
         |--hello.go               (package main  import(xyz "sample/newfolder")
         |--go mod                 (module sample go 1.15)

go mod 应该引用根文件夹,这里根文件夹是 |--sample

模块示例 转到 v1.xx

在 hello.go 内;

package main
  import ( xyz "sample/newfolder")
and make sure exported functins or variables use camelcase aka starts with blockletters.

使用模块路径导入模块内的包:

package main

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

...

Run it on the Playground

到这里,我们也就讲完了《Golang模块问题--package xxx/xxxx不在GOROOT中》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>