diff --git a/HTTP/http_client_disable_keepalive.go b/HTTP/http_client_disable_keepalive.go new file mode 100644 index 0000000..28643a7 --- /dev/null +++ b/HTTP/http_client_disable_keepalive.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "io" + "net/http" + "net/url" +) + +// 全局公用链接,请求频率比较频繁 +//var ( +// client = http.Client{ +// Transport: &http.Transport{DisableKeepAlives: false}, +// } +//) + +func main() { + apiUrl := "http://127.0.0.1:9090/query" + apiParseUrl, err := url.Parse(apiUrl) //url不带参数传入,返回一个结构体 + if err != nil { + fmt.Println("url parse err:", err) + } + data := url.Values{} + data.Set("name", "王奥") + data.Set("age", "18") //set url中带参的数据 + paramStr := data.Encode() //编码 + apiParseUrl.RawQuery = paramStr //赋值到url的结构体中,组成完整的url编码对象 + //自定义client 禁用keepalive 适用于请求频率不频繁,每次请求生成一个短链接,之后释放 + tr := http.Transport{DisableKeepAlives: true} //短链接 + client := http.Client{ + Transport: &tr, + } + //生成请求对象 + req, err := http.NewRequest("GET", apiParseUrl.String(), nil) + if err != nil { + fmt.Println("start new req err:", err) + } + //如果使用默认client: + //http.DefaultClient.Do(req) + //发送自定义请求 + resp, err := client.Do(req) + if err != nil { + return + } + defer resp.Body.Close() //关闭请求体 + //读取返回体 + body, err := io.ReadAll(resp.Body) + if err != nil { + fmt.Println("read resp body err:", err) + } + fmt.Println("resp body:", string(body)) +} diff --git a/HTTP/http_client_get.go b/HTTP/http_client_get.go new file mode 100644 index 0000000..5b7cc13 --- /dev/null +++ b/HTTP/http_client_get.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + "io" + "net/http" +) + +//http client + +func main() { + resp, err := http.Get("http://127.0.0.1:9090/query?name=sb&age=12") //没办法自定义请求参数和client + if err != nil { + return + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + return + } + fmt.Println(string(body)) +} diff --git a/HTTP/http_server.go b/HTTP/http_server.go index 6716800..8ae8387 100644 --- a/HTTP/http_server.go +++ b/HTTP/http_server.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "io" "net/http" "os" ) @@ -16,9 +17,23 @@ func f1(w http.ResponseWriter, r *http.Request) { } +func f2(w http.ResponseWriter, r *http.Request) { + //对于GET请求,query param参数都放在url,请求体中没有数据 + queryParam := r.URL.Query() + name := queryParam.Get("name") + age := queryParam.Get("age") + fmt.Println(name, age) //自动识别url中的问号中的参数 + fmt.Println(r.URL) + fmt.Println(r.Method) + fmt.Println(io.ReadAll(r.Body)) + w.Write([]byte("success")) + +} + // http server func main() { http.HandleFunc("/web", f1) + http.HandleFunc("/query", f2) http.ListenAndServe("127.0.0.1:9090", nil) } diff --git a/test/spilt_string.go b/test/spilt_string.go new file mode 100644 index 0000000..e6acb35 --- /dev/null +++ b/test/spilt_string.go @@ -0,0 +1,33 @@ +package Splitstring + +import "strings" + +//切割字符串 +//example +// abc,b =>[a,c] + +//func Split(str string, sep string) []string { +// tmp := make([]string, 0) +// for _, v := range str { +// if string(v) == sep { +// continue +// } else { +// tmp = append(tmp, string(v)) +// } +// } +// return tmp +//} + +// abc,b =>[a,c] + +func Split(str string, sep string) []string { + ret := make([]string, 0) + index := strings.Index(str, sep) + for index >= 0 { + ret = append(ret, str[:index]) + str = str[index+1:] + index = strings.Index(str, sep) + } + ret = append(ret, str) + return ret +} diff --git a/test/spilt_string_test.go b/test/spilt_string_test.go new file mode 100644 index 0000000..98947d5 --- /dev/null +++ b/test/spilt_string_test.go @@ -0,0 +1,24 @@ +package Splitstring + +import ( + "reflect" + "testing" +) + +//单元测试,测试每个函数的功能是否正常 + +func TestSplit(t *testing.T) { + got := Split("babcbef", "b") + want := []string{"", "a", "c", "ef"} + if !reflect.DeepEqual(want, got) { + t.Errorf("want:%v, but got:%v\n", want, got) + } +} + +func TestSplit2(t *testing.T) { + got := Split("a:b:c", ":") + want := []string{"a", "b", "c"} + if !reflect.DeepEqual(want, got) { + t.Errorf("want:%v, but got:%v\n", want, got) + } +}