urlA, err := url.Parse("https://example.com?name=alice&age=28&gender=female")
if err != nil {
log.Fatal(err)
}
// Make a copy of the original url.URL.
urlB := *urlA
// Make the param changes to the new url.URL type...
values := urlB.Query()
values.Add("occupation", "carpenter")
values.Del("gender")
values.Set("age", strconv.Itoa(29))
urlB.RawQuery = values.Encode()
fmt.Printf("urlA: %s\n", urlA.String()) // This will be unchanged.
fmt.Printf("urlB: %s\n", urlB.String()) // This will have the new params.
测试 Url 是否含有 String 方法
func main() {
u, _ := url.Parse("https://example.com?name=alice&age=28&gender=female")
st := reflect.TypeOf(u)
m, ok := st.MethodByName("String")
if !ok {
fmt.Println(m)
fmt.Println("don't has method string")
// method doesn't exist
} else {
fmt.Println("has method")
// do something like invoke m.F
}
}