Golang导入问题

Ele*_*tal 6 go

我在导入“golang.org/x/net/html”时遇到问题,它只是告诉我找不到该包。环顾互联网让我更加困惑,有些人说你不能再使用它了,有些人说这样做:在 Go 中安装 exp/html,有些人说只需使用“golang.org/x/net” /html”工作得很好。

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

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
}
Run Code Online (Sandbox Code Playgroud)

它显然不会让我使用 html 令牌,因为我无法导入该包。

Kir*_*ril 2

您可以查看https://golang.org/doc/code.html。它包含关于如何开始用 Go 编码的非常好的描述。你提到的问题,里面都有描述。

因此,您设置GOROOT为安装路径。然后你设置GOPATH你的 go 工作区,它遵循bin, pkg, src结构。您的第一个条目GOPATH用于存储go get ...您在计算机上安装的内容。