登录
首页 >  Golang >  Go问答

我需要做什么来执行具有如下所示的“命名”导入的示例 golang 代码?

来源:stackoverflow

时间:2024-02-12 16:18:21 477浏览 收藏

大家好,我们又见面了啊~本文《我需要做什么来执行具有如下所示的“命名”导入的示例 golang 代码?》的内容中将会涉及到等等。如果你正在学习Golang相关知识,欢迎关注我,以后会给大家带来更多Golang相关文章,希望我们能一起进步!下面就开始本文的正式内容~

问题内容

这是新手问题。依赖项似乎在github上,从导入中很明显,那么为什么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
}

正确答案


在名为 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

今天关于《我需要做什么来执行具有如下所示的“命名”导入的示例 golang 代码?》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

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