Golang导入问题

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

Golang import issue

问题

我遇到了导入"golang.org/x/net/html"的问题,它告诉我找不到该包。在互联网上搜索只让我更加困惑,有些人说你不能再使用它了,有些人说要按照这个链接的方法操作:https://stackoverflow.com/questions/18888093/install-exp-html-in-go,还有些人说只需使用"golang.org/x/net/html"就可以正常工作。

这是我尝试运行的代码的一小部分:

package main


import (
    "fmt"
    "html"
    "net/http"
    "golang.org/x/net/html"
    "os"
    "strings"
)

// 从Token中获取href属性的辅助函数
func getHref(t html.Token) (ok bool, href string) {
    // 遍历Token的所有属性,直到找到一个"href"
    for _, a := range t.Attr {
        if a.Key == "href" {
            href = a.Val
            ok = true
        }
    }

    return
}

显然,它不允许我使用html token,因为我无法导入该包。

英文:

I'm having issues importing "golang.org/x/net/html" it's simply telling me it can't find the package. Looking around the internet has only confused me more with some people saying that you can't use it anymore, some people saying to do this: https://stackoverflow.com/questions/18888093/install-exp-html-in-go and some people saying that just using "golang.org/x/net/html" works just fine.

Here is just a small part of the code I'm trying to run:

package main


import (
    "fmt"
    "html"
    "net/http"
    "golang.org/x/net/html"
    "os"
    "strings"
)

// Helper function to pull the href attribute from a Token
func getHref(t html.Token) (ok bool, href string) {
    // Iterate over all of the Token's attributes until we find an "href"
    for _, a := range t.Attr {
        if a.Key == "href" {
            href = a.Val
            ok = true
        }
    }

    return
}

It obviously won't let me use the html token because I can't import the package.

答案1

得分: 3

你可以在命令行中执行以下命令:"go get golang.org/x/net/html"

英文:

You can execute at the command line “go get golang.org/x/net/html”

答案2

得分: 2

你可以查看https://golang.org/doc/code.html。它包含了关于如何开始使用Go进行编码的非常好的描述。你提到的问题在那里有详细说明。

所以,你需要将GOROOT设置为你的安装路径。然后将GOPATH设置为你的Go工作空间,它遵循bin, pkg, src的结构。在你的GOPATH中的第一个条目用于存储你在机器上安装的go get ...命令。

英文:

You can take a look at https://golang.org/doc/code.html. It contains a very good description on how to start coding in Go. The issues you are mentioning are described there.

So, you set GOROOT to your installation path. Then you set GOPATH to your go workspace(s), which obey the bin, pkg, src structure. The first entry in your GOPATH is used to store the go get ... which you install on your machine.

huangapple
  • 本文由 发表于 2015年5月26日 04:30:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/30445622.html
匿名

发表评论

匿名网友

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

确定