英文:
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"
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论