无法将baseURL(类型为*url.URL)作为http.Get的参数中的字符串类型使用。

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

cannot use baseURL (type *url.URL) as type string in argument to http.Get

问题

我写了一个简单的Web服务器,使用url.ResolveReference将一些相对路径附加到基本URL上。然后,我想使用http.Get()获取结果URL的内容,但问题是http.Get()接受字符串作为参数,而我有一个*url.URL类型的对象。我该如何解决这个问题?

以下是我的代码:

package main

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

func factHandler(w http.ResponseWriter, r *http.Request) {
    str1 := "http://www.meaningfultype.com/"
    u1, _ := url.Parse(str1)
    str2 := "/clarendon.html"
    u2, _ := url.Parse(str2)
    u3 := u1.ResolveReference(u2)
    fmt.Println("url:", u3)
    res, err := http.Get(u3)
    if err != nil {
        log.Fatal(err)
    }
    robots, err := ioutil.ReadAll(res.Body)

    res.Body.Close()
    if err != nil {
        log.Fatal(err)
    }
    contentType := res.Header.Get("Content-Type")
    w.Header().Set("Content-Type", contentType)
    w.Write(robots)
}

希望能帮到你!

英文:

I wrote a simple web server that appends some relative path to the base URL using url.ResolveReference. And then I want to get the content of resultant URL using http.Get() but the problem is that http.get() take string as the argument and I have an object of type *url.URL.How can I resolve this problem?

My code is given bellow:

package main

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

func factHandler(w http.ResponseWriter, r *http.Request) {
    str1 := "http://www.meaningfultype.com/"
    u1, _ := url.Parse(str1)
    str2 := "/clarendon.html"
    u2, _ := url.Parse(str2)
    u3 := u1.ResolveReference(u2)
    fmt.Println("url:", u3)
    res, err := http.Get(u3)
    if err != nil {
        log.Fatal(err)
    }
    robots, err := ioutil.ReadAll(res.Body)

    res.Body.Close()
    if err != nil {
        log.Fatal(err)
    }
    contentType := res.Header.Get("Content-Type")
    w.Header().Set("Content-Type", contentType)
    w.Write(robots)
}

答案1

得分: 1

调用u3String()方法,并将返回的字符串传递给http.Get()

package main

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

func factHandler(w http.ResponseWriter, r *http.Request) {
    str1 := "http://www.meaningfultype.com/"
    u1, _ := url.Parse(str1)
    str2 := "/clarendon.html"
    u2, _ := url.Parse(str2)
    u3 := u1.ResolveReference(u2)
    fmt.Println("url:", u3)
    res, err := http.Get(u3.String())         // 更改此行
    if err != nil {
        log.Fatal(err)
    }
    robots, err := ioutil.ReadAll(res.Body)

    res.Body.Close()
    if err != nil {
        log.Fatal(err)
    }
    contentType := res.Header.Get("Content-Type")
    w.Header().Set("Content-Type", contentType)
    w.Write(robots)
}

请注意,这是一段Go语言代码。

英文:

Call the String() method of u3 and pass the returned string to http.Get():

package main

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

func factHandler(w http.ResponseWriter, r *http.Request) {
    str1 := "http://www.meaningfultype.com/"
    u1, _ := url.Parse(str1)
    str2 := "/clarendon.html"
    u2, _ := url.Parse(str2)
    u3 := u1.ResolveReference(u2)
    fmt.Println("url:", u3)
    res, err := http.Get(u3.String())         // Change this line
    if err != nil {
        log.Fatal(err)
    }
    robots, err := ioutil.ReadAll(res.Body)

    res.Body.Close()
    if err != nil {
        log.Fatal(err)
    }
    contentType := res.Header.Get("Content-Type")
    w.Header().Set("Content-Type", contentType)
    w.Write(robots)
}

huangapple
  • 本文由 发表于 2017年4月10日 23:01:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/43326613.html
匿名

发表评论

匿名网友

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

确定