Golang “net/http” 错误:undefined: DetectContentType

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

Golang "net/http" Error: undefined: DetectContentType

问题

"net/http" 包实现了函数 DetectContentType

http://golang.org/pkg/net/http/#DetectContentType

但是当我尝试使用它时,我得到了错误 undefined: DetectContentType

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. )
  8. func main() {
  9. res, err := http.Get("http://www.google.com/robots.txt")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. robots, err := ioutil.ReadAll(res.Body)
  14. res.Body.Close()
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. fileType := DetectContentType(robots)
  19. fmt.Println("FileType: ", fileType)
  20. }

https://play.golang.org/p/WJDuU8Xx-5

我做错了什么?

英文:

The "net/http" package implements the function DetectContentType

http://golang.org/pkg/net/http/#DetectContentType

but when I try to use it, i get the error undefined: DetectContentType

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. )
  8. func main() {
  9. res, err := http.Get("http://www.google.com/robots.txt")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. robots, err := ioutil.ReadAll(res.Body)
  14. res.Body.Close()
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. fileType := DetectContentType(robots)
  19. fmt.Println("FileType: ", fileType)
  20. }

https://play.golang.org/p/WJDuU8Xx-5

What am I doing wrong?

答案1

得分: 4

这是要翻译的内容:

这是“net/http”包的一部分,因此您需要编写以下代码:

  1. http.DetectContentType(robots)

请注意,如果您想以这种方式使用此函数,您可以更改导入语句如下:

  1. import (
  2. ...
  3. . "net/http"
  4. )
英文:

It is part of the "net/http" package therefore you have to write,

  1. http.DetectContentType(robots)

Note that if you wanted to use this function in this way you could change your import statement as such:

  1. import (
  2. ...
  3. . "net/http"
  4. )

huangapple
  • 本文由 发表于 2014年12月2日 09:38:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/27240479.html
匿名

发表评论

匿名网友

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

确定