dragon8002 2014-12-02 01:38
浏览 149
已采纳

Golang“ net / http”错误:未定义:DetectContentType

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条回答 默认 最新

  • douan3019 2014-12-02 01:41
    关注

    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"
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)