gpt4 book ai didi

Golang 模块问题--package xxx/xxxx is not in GOROOT

转载 作者:行者123 更新时间:2023-12-05 02:48:36 24 4
gpt4 key购买 nike

所以这是我的目录:

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

当给它 cmd go run hello.go 时,终端出来是这样的:

D:\Users\eien_zheng\go\src\ppppppSample>go run hello.gohello.go:5:2: 包 ppppppSample/newFolderOne 不在 GOROOT 中 (C:\Go\src\ppppppSample\newFolderOne)

所以这是我的问题:

(由于本人刚接触golang,希望大家能理解并容忍我的一些误会)

根据我对Go module的理解(可能是错误的),Go module的功能是让某种在线资源下载到目录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

module samplego v1.xx

inside hello.go;

  package main
import ( xyz "sample/newFolder")

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

关于Golang 模块问题--package xxx/xxxx is not in GOROOT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64383545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com