duanbai5348 2016-02-22 19:01
浏览 93
已采纳

用golang代理的变量替换字符串[关闭]

I'm trying to use the http proxy code here

proxyUrl := url.Parse(strings.Replace("%v", RandomProxyAddress()))
http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)}

Tells me there are too many arguments in the url.Parse

But when I try

proxyUrl := url.Parse(RandomProxyAddress())
http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)}

I get ./main.go:138: multiple-value url.Parse() in single-value context

When I tried a string replace it also tells me too many varibles. Not sure how to get my proxy to work with the url.Parse

  • 写回答

1条回答 默认 最新

  • dta38159 2016-02-22 19:13
    关注

    Firstly, url.Parse returns two variables, a URL and an error, as per the docs here.

    Secondly, instead of strings.Replace you probably meant to use fmt.Sprintf("%v", RandomProxyAddress()), assuming RandomProxyAddress() returns a string, or something that formats into the string you want. So all in all, you should have:

    addr := fmt.Sprintf("%v", RandomProxyAddress())
    proxyURL, err := url.Parse(addr)
    if err != nil {
        log.Println(err)
    }
    

    That said, the fmt.Sprintf would be unnecessary if RandomProxyAddress() already returns a string. Check the docs for fmt and fmt.Sprintf. And if you really did mean to use strings.Replace, notice that it requires four arguments, not two.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接