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

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

Golang "net/http" Error: undefined: DetectContentType

问题

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

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

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

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
)

func main() {
	res, err := http.Get("http://www.google.com/robots.txt")
	if err != nil {
		log.Fatal(err)
	}
	
	robots, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		log.Fatal(err)
	}
	
	fileType := DetectContentType(robots)
	
        fmt.Println("FileType: ", fileType)
}

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

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
)

func main() {
	res, err := http.Get("http://www.google.com/robots.txt")
	if err != nil {
		log.Fatal(err)
	}
	
	robots, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		log.Fatal(err)
	}
	
	fileType := DetectContentType(robots)
	
        fmt.Println("FileType: ", fileType)
}

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

What am I doing wrong?

答案1

得分: 4

这是要翻译的内容:

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

http.DetectContentType(robots)

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

import (
   ...
   . "net/http"
)
英文:

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

http.DetectContentType(robots)

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

import (
   ...
   . "net/http"
)

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:

确定