此代碼是從大型代碼庫中嘗試複製錯誤的獨立示例。當這個程序運行時,&request.URL.Host
和&request1.URL.Host
的地址是相同的。爲什麼?從我的理解,這是2個不同的結構,所以URL.Host
不應該有相同的地址。爲什麼http.Request.URL.Host的地址與2個不同的http.Request結構相同?
package main
import (
"crypto/tls"
"fmt"
"net/http"
"net/url"
)
func main() {
hostname := "www.google.com"
uri, err := url.Parse("http://www.google.com/")
if err != nil {
panic(err)
}
var tlsConfig *tls.Config
tlsConfig = &tls.Config{
ServerName: hostname,
InsecureSkipVerify: true,
}
client := &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
TLSClientConfig: tlsConfig,
},
}
request1 := &http.Request{
Header: http.Header{"User-Agent": {"Foo"}},
Host: hostname,
Method: "GET",
URL: uri,
}
request2 := &http.Request{
Header: http.Header{"User-Agent": {"Foo"}},
Host: hostname,
Method: "GET",
URL: uri,
}
fmt.Printf("Address1: %s, Address2: %s\n", &request1.URL.Host, &request2.URL.Host)
resp, err := client.Do(request1)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Printf("\nResponse: %s", resp)
}
你給他們分配相同的價值,他們爲什麼會不同? – JimB