你可以如何使用库对象?

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

How can I use library object?

问题

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

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

func getLocalFile(filename string) (*Document) { // 错误
	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)
}
英文:

I have an error.
Document is struct object on goquery library.
I can't use it in my code below. How can I do?

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)
}

答案1

得分: 4

《Go编程语言规范》

限定标识符

限定标识符是带有包名前缀的标识符。包名和标识符都不能是空白。

QualifiedIdent = PackageName "." identifier

限定标识符用于访问不同包中的标识符,该包必须被导入。该标识符必须是被导出的,并且在该包的包块中声明。

math.Sin  // 表示math包中的Sin函数

使用完全限定名称: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)
}
英文:

> 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

Use the fully qualified name: goquery.Document. For example,

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)
}

huangapple
  • 本文由 发表于 2015年10月18日 14:39:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/33195072.html
匿名

发表评论

匿名网友

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

确定