• go build报错,提示错误can‘t load package: package xxx is not in GOROOT


    我修改了一下 go.mod 文件 

    lic_req 改成了 lic_req0506
     
    module lic_req0506
    
    go 1.17
    
    require (
        github.com/farmerx/gorsa v0.0.0-20161211100049-3ae06f674f40
        github.com/gin-gonic/gin v1.7.7
    )
    
    require (
        github.com/gin-contrib/sse v0.1.0 // indirect
        github.com/go-playground/locales v0.13.0 // indirect
        github.com/go-playground/universal-translator v0.17.0 // indirect
        github.com/go-playground/validator/v10 v10.4.1 // indirect
        github.com/golang/protobuf v1.3.3 // indirect
        github.com/json-iterator/go v1.1.9 // indirect
        github.com/leodido/go-urn v1.2.0 // indirect
        github.com/mattn/go-isatty v0.0.12 // indirect
        github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
        github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
        github.com/ugorji/go/codec v1.1.7 // indirect
        golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
        golang.org/x/sys v0.0.0-20200116001909-b77594299b42 // indirect
        gopkg.in/yaml.v2 v2.2.8 // indirect
    )

     编译就不行了:

    PS C:\code\luok\requesttoolgo> go build
    main.go:10:2: package lic_req/httpUtils is not in GOROOT (C:\Program Files (x86)\Go\src\lic_req\httpUtils)
    main.go:11:2: package lic_req/securityUtils is not in GOROOT (C:\Program Files (x86)\Go\src\lic_req\securityUtils)

    检查发现有错误:

    为什么啊!

    找了一大圈都不行!

    看看环境变量:

    PS C:\code\luok\requesttoolgo> go env

    set GO111MODULE=
    set GOARCH=386
    set GOBIN=
    set GOCACHE=C:\Users\luok\AppData\Local\go-build
    set GOENV=C:\Users\luok\AppData\Roaming\go\env
    set GOEXE=.exe
    set GOEXPERIMENT=
    set GOFLAGS=
    set GOHOSTARCH=386
    set GOHOSTOS=windows
    set GOINSECURE=
    set GOMODCACHE=C:\Users\luok\go\pkg\mod
    set GONOPROXY=
    set GONOSUMDB=
    set GOOS=windows
    set GOPATH=C:\Users\luok\go
    set GOPRIVATE=
    set GOSUMDB=sum.golang.org
    set GOVERSION=go1.18.1
    set GCCGO=gccgo
    set AR=ar
    set CC=gcc
    set CGO_CFLAGS=-g -O2

    PS C:\code\luok\requesttoolgo> go env GOROOT
    C:\Program Files (x86)\Go

    go mod 问题?

    重新初始化一下:

    PS C:\code\luok\requesttoolgo> go mod init
    main.go:10:2: package lic_req/httpUtils is not in GOROOT (C:\Program Files (x86)\Go\src\lic_req\httpUtils)
    main.go:11:2: package lic_req/securityUtils is not in GOROOT (C:\Program Files (x86)\Go\src\lic_req\securityUtils)
    PS C:\code\luok\requesttoolgo> go build
    main.go:10:2: package lic_req/httpUtils is not in GOROOT (C:\Program Files (x86)\Go\src\lic_req\httpUtils)
    main.go:11:2: package lic_req/securityUtils is not in GOROOT (C:\Program Files (x86)\Go\src\lic_req\securityUtils)
    PS C:\code\luok\requesttoolgo>
    PS C:\code\luok\requesttoolgo> go mod tidy
    PS C:\code\luok\requesttoolgo>
    PS C:\code\luok\requesttoolgo> go mod init
    go: C:\code\luok\requesttoolgo\go.mod already exists

    还是不行啊!go mod 模式改为传统模式:

    PS C:\code\luok\requesttoolgo> go env -w GO111MODULE=   

    或者

    PS C:\code\luok\requesttoolgo> go env -w GO111MODULE=off

    也是不行:

    PS C:\code\luok\requesttoolgo> go build
    main.go:17:2: cannot find package "github.com/gin-gonic/gin" in any of:
    C:\Program Files (x86)\Go\src\github.com\gin-gonic\gin (from $GOROOT)
    C:\Users\luok\go\src\github.com\gin-gonic\gin (from $GOPATH)
    main.go:10:2: cannot find package "lic_req/httpUtils" in any of:
    C:\Program Files (x86)\Go\src\lic_req\httpUtils (from $GOROOT)
    C:\Users\luok\go\src\lic_req\httpUtils (from $GOPATH)
    main.go:11:2: cannot find package "lic_req/securityUtils" in any of:
    C:\Program Files (x86)\Go\src\lic_req\securityUtils (from $GOROOT)
    C:\Users\luok\go\src\lic_req\securityUtils (from $GOPATH)

    改回去吧:  PS C:\code\luok\requesttoolgo> go env -w GO111MODULE=on

    后面终于解决了!!发现, 就是 需要go.mod 文件里面的 module 和 import 里面的保持一致!

    go.mod 文件里面的 module 字段不能随便改!

  • 相关阅读:
    性能测试
    Oracle Install logs
    运维服务方案
    对字符串的查找,剪切,替换,提取(正则表达式)
    用java程序模拟网站的登录以及文件批量上传
    HDU_1542_(树状数组)
    HDU_3792_(素数筛+树状数组)
    Codeforces_791_B. Bear and Friendship Condition_(dfs)
    POJ_1125_(dijkstra)
    POJ_1088_(dp)(记忆化搜索)
  • 原文地址:https://www.cnblogs.com/FlyAway2013/p/16230249.html
Copyright © 2020-2023  润新知