【问题标题】:What do I need to do to execute sample golang code having a 'named' import like below?我需要做什么来执行具有如下“命名”导入的示例 golang 代码?
【发布时间】:2021-10-30 00:03:53
【问题描述】:

这是新手问题。依赖好像在github上,从import上看就很明显了,为什么run不行呢?

错误是:没有必需的模块提供包 github.com/hashicorp/go-getter

    package main

import (
    "context"
    "fmt"
    "os"
// Problem with line below, getting error: no required module provides package
    getter "github.com/hashicorp/go-getter"
)

func main() {
    client := &getter.Client{
        Ctx: context.Background(),
        //define the destination to where the directory will be stored. This will create the directory if it doesnt exist
        Dst: "/tmp/gogetter",
        Dir: true,
        //the repository with a subdirectory I would like to clone only
        Src:  "github.com/hashicorp/terraform/examples/cross-provider",
        Mode: getter.ClientModeDir,
        //define the type of detectors go getter should use, in this case only github is needed
        Detectors: []getter.Detector{
            &getter.GitHubDetector{},
        },
        //provide the getter needed to download the files
        Getters: map[string]getter.Getter{
            "git": &getter.GitGetter{},
        },
    }
    //download the files
    if err := client.Get(); err != nil {
        fmt.Fprintf(os.Stderr, "Error getting path %s: %v", client.Src, err)
        os.Exit(1)
    }
    //now you should check your temp directory for the files to see if they exist
}

【问题讨论】:

  • 您使用的是什么版本。也许您正在使用 go mod,在这种情况下,请按照以下答案来初始化包并获取其依赖项。应该这样做。

标签: go


【解决方案1】:

在某处创建一个名为getter 的文件夹,然后创建一个文件 getter/getter.go:

package main

import (
   "fmt"
   "github.com/hashicorp/go-getter/v2"
)

func main() {
   fmt.Println(getter.ErrUnauthorized)
}

请注意,我没有使用您指定的名称,因为在这种情况下它是多余的。该包已被称为getter [1],因此您无需指定相同的名称。然后,运行:

go mod init getter
go mod tidy
go build
  1. https://pkg.go.dev/github.com/hashicorp/go-getter/v2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    相关资源
    最近更新 更多