所以,我正在使用net/http包.我正在获取一个我确定知道重定向的URL.在登陆最终URL之前,它甚至可能会重定向几次.重定向在后台自动处理.
有没有简单的方法来确定最终的URL是什么,没有涉及在http.Client对象上设置CheckRedirect字段的hackish变通方法?
我想我应该提一下,我认为我想出了一个解决方法,但它有点像hackish,因为它涉及使用全局变量并在自定义http.Client上设置CheckRedirect字段.
必须有一个更清洁的方法来做到这一点.我希望这样的事情:
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
// Try to GET some URL that redirects. Could be 5 or 6 unseen redirections here.
resp, err := http.Get("http://some-server.com/a/url/that/redirects.html")
if err != nil {
log.Fatalf("http.Get => %v", err.Error())
}
// Find out what URL we ended up at
finalURL := magicFunctionThatTellsMeTheFinalURL(resp)
fmt.Printf("The URL you ended up at is: %v", finalURL)
}
Run Code Online (Sandbox Code Playgroud) 我对 golang 相当陌生,所以我确信这个问题很大程度上是由于我的一些概念缺陷造成的。
在 golang 中,我们可以有两种类型的通道:无缓冲和缓冲(分别是同步和异步)。
unbufferedChan := make(chan string)
bufferedChan := make(chan string, 100)
Run Code Online (Sandbox Code Playgroud)
通过无缓冲通道进行通信的两个 goroutine 必须相互等待。也就是说,接收 Goroutine 会阻塞,直到发送者发送,发送者也会阻塞,直到接收者接收。
在缓冲的情况下,仅当通道为空时接收器才会阻塞。仅当通道已满时,发送方才会阻塞。
通过使用缓冲通道,我希望减少 goroutine 的阻塞时间,从而提高代码的速度。相反,一切都变慢了。与我的预期相反(即“缓冲更高效”),无缓冲通道最终速度要快得多。
这是所有代码。你首先需要这样做go get code.google.com/p/go-html-transform/html/transform。
package main
import (
"fmt"
"math/rand"
"os"
"sync"
"time"
sel "code.google.com/p/go-html-transform/css/selector"
h5 "code.google.com/p/go-html-transform/h5"
gnhtml "code.google.com/p/go.net/html"
)
// Find a specific HTML element and return its textual element children.
func main() {
test := `
<html>
<head>
<title>This is the test document!</title>
<style>
header: color=blue;
</style>
</head>
<body>
<div id="h" class="header">This …Run Code Online (Sandbox Code Playgroud)