你需要做什么才能执行具有类似下面的“命名”导入的示例golang代码?

huangapple go评论76阅读模式
英文:

What do I need to do to execute sample golang code having a 'named' import like below?

问题

这是一个新手问题。依赖项似乎在GitHub上,从导入语句中很明显,那为什么运行不起来呢?

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

package main

import (
	"context"
	"fmt"
	"os"
	// 下面这行代码有问题,出现错误:没有提供所需的模块
	getter "github.com/hashicorp/go-getter"
)

func main() {
	client := &getter.Client{
		Ctx: context.Background(),
		// 定义目标目录,如果目录不存在则创建
		Dst: "/tmp/gogetter",
		Dir: true,
		// 要克隆的仓库及其子目录
		Src:  "github.com/hashicorp/terraform/examples/cross-provider",
		Mode: getter.ClientModeDir,
		// 定义要使用的检测器类型,这里只需要 GitHub
		Detectors: []getter.Detector{
			&getter.GitHubDetector{},
		},
		// 提供下载文件所需的 getter
		Getters: map[string]getter.Getter{
			"git": &getter.GitGetter{},
		},
	}
	// 下载文件
	if err := client.Get(); err != nil {
		fmt.Fprintf(os.Stderr, "获取路径 %s 时出错:%v", client.Src, err)
		os.Exit(1)
	}
	// 现在你可以检查临时目录中的文件是否存在
}
英文:

This is newbie question. The dependencies seems to be on github, and it's pretty obvious from the import, so why run doesn't work?

Error is: no required module provides package 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
}

答案1

得分: 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
英文:

Create a folder somewhere called getter, then create a file
getter/getter.go:

package main

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

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

Notice I didn't use a name like you specified, as it's redundant in this case. The package is already called getter [1], so you don't need to specify the same name. Then, run:

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

huangapple
  • 本文由 发表于 2021年7月7日 02:51:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/68276013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定