你可以如何使用库对象?

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

How can I use library object?

问题

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

  1. package main
  2. import (
  3. "log"
  4. "github.com/PuerkitoBio/goquery"
  5. "os"
  6. )
  7. func getLocalFile(filename string) (*Document) { // 错误
  8. f, e := os.Open(FILTER_FILE)
  9. if e != nil {
  10. log.Fatal(e)
  11. }
  12. defer f.Close()
  13. doc, e := goquery.NewDocumentFromReader(f)
  14. if e != nil {
  15. log.Fatal(e)
  16. }
  17. return doc
  18. }
  19. const FILE_NAME = "input.html"
  20. func main() {
  21. doc := getLocalFile(FILE_NAME)
  22. println(doc)
  23. }
英文:

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

  1. package main
  2. import (
  3. "log"
  4. "github.com/PuerkitoBio/goquery"
  5. "os"
  6. )
  7. func getLocalFile(filename string) (*Document) { // Error
  8. f, e := os.Open(FILTER_FILE)
  9. if e != nil {
  10. log.Fatal(e)
  11. }
  12. defer f.Close()
  13. doc, e := goquery.NewDocumentFromReader(f)
  14. if e != nil {
  15. log.Fatal(e)
  16. }
  17. return doc
  18. }
  19. const FILE_NAME = "input.html"
  20. func main() {
  21. doc := getLocalFile(FILE_NAME)
  22. println(doc)
  23. }

答案1

得分: 4

《Go编程语言规范》

限定标识符

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

  1. QualifiedIdent = PackageName "." identifier

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

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

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

  1. package main
  2. import (
  3. "github.com/PuerkitoBio/goquery"
  4. "log"
  5. "os"
  6. )
  7. func getLocalFile(filename string) *goquery.Document {
  8. f, e := os.Open(filename)
  9. if e != nil {
  10. log.Fatal(e)
  11. }
  12. defer f.Close()
  13. doc, e := goquery.NewDocumentFromReader(f)
  14. if e != nil {
  15. log.Fatal(e)
  16. }
  17. return doc
  18. }
  19. const FILE_NAME = "input.html"
  20. func main() {
  21. doc := getLocalFile(FILE_NAME)
  22. println(doc)
  23. }
英文:

> 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,

  1. package main
  2. import (
  3. "github.com/PuerkitoBio/goquery"
  4. "log"
  5. "os"
  6. )
  7. func getLocalFile(filename string) *goquery.Document {
  8. f, e := os.Open(filename)
  9. if e != nil {
  10. log.Fatal(e)
  11. }
  12. defer f.Close()
  13. doc, e := goquery.NewDocumentFromReader(f)
  14. if e != nil {
  15. log.Fatal(e)
  16. }
  17. return doc
  18. }
  19. const FILE_NAME = "input.html"
  20. func main() {
  21. doc := getLocalFile(FILE_NAME)
  22. println(doc)
  23. }

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:

确定