gpt4 book ai didi

go - 如何使用库对象?

转载 作者:IT王子 更新时间:2023-10-29 02:29:56 26 4
gpt4 key购买 nike

我有一个错误。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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com