【问题标题】:Go package not in $GOROOTGo 包不在 $GOROOT 中
【发布时间】:2021-10-31 12:31:43
【问题描述】:

我正在使用以下 Go 模块来构建应用程序:

github.com/martinlindhe/unit

现在,Go 代码非常简单;我只是想为实际工作设置环境:

import(
    "fmt"
    "unit"
    
)

foo := unit.FromFahrenheit(100)
fmt.Println("100 fahrenheit in celsius = ", foo.Celsius())

在 go.mod 中:


go 1.17

require github.com/martinlindhe/unit v0.0.0-20210313160520-19b60e03648d

执行go buildgo get 会导致:

package unit is not in GOROOT (/usr/local/Cellar/go/1.17/libexec/src/unit)

运行go mod download 执行没有错误。 go.sum 文件似乎是正确的,所有必要的依赖项都存在。

环境是最新版本的VS Code,Go通过homebrew安装在MacOS Big Sur 11.5.2上

一定有一些明显的东西我错过了。我编写的其他应用程序没有这个问题。

【问题讨论】:

  • 将其导入为“github.com/martinlindhe/unit”,而不仅仅是“unit”
  • 啊哈!谢谢!昨晚我这样做太晚了。

标签: go


【解决方案1】:

导入路径不对。 Playground.

package main

import (
        "fmt"
        "github.com/martinlindhe/unit"
)

func main() {
        foo := unit.FromFahrenheit(100)
        fmt.Println("100 fahrenheit in celsius = ", foo.Celsius())

}

【讨论】:

    【解决方案2】:

    我已经在我的电脑上测试过这个问题。正如我在运行命令后看到的那样(“go get -u github.com/martinlindhe/unit”)它没有在 src 目录中添加包。我已经手动添加了它,然后它就可以工作了。您可以从 github 下载该软件包并将其放在您的 (/usr/local/Cellar/go/1.17/libexec/src) 特定目录中并运行此代码。我希望它会工作。

    package main
    
    import(
       "fmt"
       "unit"
    )
    
    func main(){
      foo := unit.FromFahrenheit(100)
      fmt.Println("100 fahrenheit in celsius = ", foo.Celsius())
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 2022-09-28
      • 2021-01-30
      • 2021-04-10
      相关资源
      最近更新 更多