go - 如何使用库对象?

标签 go

我有一个错误。 Document 是 goquery 库中的结构对象。 我不能在下面的代码中使用它。我该怎么办?

package main
import (
    "log"
    "github.com/PuerkitoBio/goquery"
    "os"
)

func getLocalFile(filename string) (*Document) { // Error
    f, e := os.Open(FILTER_FILE)
    if e != nil {
        log.Fatal(e)
    }
    defer f.Close()

    doc, e := goquery.NewDocumentFromReader(f)
    if e != nil {
        log.Fatal(e)
    }
    return doc
}

const FILE_NAME = "input.html"
func main() {
    doc := getLocalFile(FILE_NAME)
    println(doc)
}

最佳答案

The Go Programming Language Specification

Qualified identifiers

A qualified identifier is an identifier qualified with a package name prefix. Both the package name and the identifier must not be blank.

QualifiedIdent = PackageName "." identifier .

A qualified identifier accesses an identifier in a different package, which must be imported. The identifier must be exported and declared in the package block of that package.

math.Sin  // denotes the Sin function in package math

使用完全限定名称:goquery.Document。例如,

package main

import (
    "github.com/PuerkitoBio/goquery"
    "log"
    "os"
)

func getLocalFile(filename string) *goquery.Document {
    f, e := os.Open(filename)
    if e != nil {
        log.Fatal(e)
    }
    defer f.Close()

    doc, e := goquery.NewDocumentFromReader(f)
    if e != nil {
        log.Fatal(e)
    }
    return doc
}

const FILE_NAME = "input.html"

func main() {
    doc := getLocalFile(FILE_NAME)
    println(doc)
}

关于go - 如何使用库对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33195072/

相关文章:

go - 如何对订单经常更新的提要进行分页?

url - 在 Go 中转换相对于绝对 URL

go - 将结构字段名称声明为 "type"

json - 如何在 Go 中将 byte/uint8 数组编码为 json 数组?

pointers - 如何跨包中的文件使用全局变量?

go - 带参数的 Dialogflow 响应

sorting - go 中相对于另一个 slice 对 slice 进行排序

regex - 带行首的正则表达式

go - 组合非类型化和类型化常量时的类型推断

amazon-web-services - 如何通过 HTTP 获取和存储到 S3 操作共享 session ?