在标准包Windows中使用非标准导入。

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

non-standard import in standard package windows

问题

我无法运行我的Go代码,因为出现了以下错误:

> 标准包“goprojects/search”中的非标准导入“gopkg.in/olivere/elastic.v5”

Main.go位于:C:\Go\src\goprojects\search\main.go

GOROOT为C:\Go

GOPATH为C:\Go\src\goprojects(尝试将当前工作目录\search添加到此处,但没有帮助)

当我运行“go get gopkg.in/olivere/elastic.v5”时,我在C:\Go\src\goprojects\src\gopkg.in\olivere\elastic.v5中找到了导入的文件

Visual Studio Code给我显示了以下消息:

> 在以下位置找不到包“go.pkg.in/olivere/elastic.v5”:

> C:\Go\src\vendor\gopkg.in\olivere\elastic.v5(供应商树)

> C:\Go\src\gopkg.in\olivere\elastic.v5(来自$GOROOT)

> C:\Go\src\goprojects\src\gopkg.in\olivere\elastic.v5(来自$GOPATH)

我的代码:

package main

import (
	"fmt"
	"net/http"

	elastic "gopkg.in/olivere/elastic.v5"
)

func main() {
	http.HandleFunc("/search", search)
	http.ListenAndServe(":3000", nil)
}

func search(w http.ResponseWriter, r *http.Request) {
	searchString := r.URL.Query().Get("q")
	fmt.Println("Searching for" + searchString)

	// 创建一个客户端
	client, err := elastic.NewClient()

	w.Write([]byte(searchString))
}
英文:

I can't seem to run my go code because of this error

> non-standard import "gopkg.in/olivere/elastic.v5" in standard package "goprojects/search"

Main.go is located at: C:\Go\src\goprojects\search\main.go

GOROOT is C:\Go

GOPATH is C:\Go\src\goprojects (tried adding my current working directory here \search, but didnt help)

when I run "go get gopkg.in/olivere/elastic.v5" I get the imported files in C:\Go\src\goprojects\src\gopkg.in\olivere\elastic.v5

visual studio code is giving me this message

> cannot find package "go.pkg.in/olivere/elastic.v5" in any of:

> C:\Go\src\vendor\gopkg.in\olivere\elastic.v5 (vendor tree)

> C:\Go\src\gopkg.in\olivere\elastic.v5 (from $GOROOT)

> C:\Go\src\goprojects\src\gopkg.in\olivere\elastic.v5 (from $GOPATH)

my code

package main

import (
	"fmt"
	"net/http"

	elastic "gopkg.in/olivere/elastic.v5"
)

func main() {
	http.HandleFunc("/search", search)
	http.ListenAndServe(":3000", nil)
}

func search(w http.ResponseWriter, r *http.Request) {
	searchString := r.URL.Query().Get("q")
	fmt.Println("Searching for" + searchString)

	// Create a client
	client, err := elastic.NewClient()

	w.Write([]byte(searchString))
}

答案1

得分: 5

我将我的 Go 项目从 GoRoot 路径移动到了 C:\goprojects 的一个单独文件夹中,并用新路径替换了我的 GoPath,这样就成功了。

英文:

moved my goprojects out of my GoRoot path and into a separate folder in C:\goprojects and replaced my GoPath with the new path and it worked.

huangapple
  • 本文由 发表于 2017年9月10日 20:01:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/46140491.html
匿名

发表评论

匿名网友

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

确定