目录
一、直接调用两个翻译函数
1.完整代码
2.运行时间
二、并行后
1.完整代码
2.运行时间
一、直接调用两个翻译函数
1.完整代码
package main import ( "bytes" "encoding/json" "fmt" "io/ioutil" "log" "net/http" // "strings" "os" "time" ) type DictResponse struct { Words []struct { Source int `json:"source"` Text string `json:"text"` PosList []struct { Type int `json:"type"` Phonetics []struct { Type int `json:"type"` Text string `json:"text"` } `json:"phonetics"` Explanations []struct { Text string `json:"text"` Examples []struct { Type int `json:"type"` Sentences []struct { Text string `json:"text"` TransText string `json:"trans_text"` } `json:"sentences"` } `json:"examples"` Synonyms []interface{} `json:"synonyms"` } `json:"explanations"` Relevancys []interface{} `json:"relevancys"` } `json:"pos_list"` } `json:"words"` Phrases []interface{} `json:"phrases"` BaseResp struct { StatusCode int `json:"status_code"` StatusMessage string `json:"status_message"` } `json:"base_resp"` } type DictRequest struct { Text string `json:"text"` Language string `json:"language"` } type DictResponse2 struct { Rc int `json:"rc"` Wiki struct { KnownInLaguages int `json:"known_in_laguages"` Description struct { Source string `json:"source"` Target interface{} `json:"target"` } `json:"description"` ID string `json:"id"` Item struct { Source string `json:"source"` Target string `json:"target"` } `json:"item"` ImageURL string `json:"image_url"` IsSubject string `json:"is_subject"` Sitelink string `json:"sitelink"` } `json:"wiki"` Dictionary struct { Prons struct { EnUs string `json:"en-us"` En string `json:"en"` } `json:"prons"` Explanations []string `json:"explanations"` Synonym []string `json:"synonym"` Antonym []string `json:"antonym"` WqxExample [][]string `json:"wqx_example"` Entry string `json:"entry"` Type string `json:"type"` Related []interface{} `json:"related"` Source string `json:"source"` } `json:"dictionary"` } type DictRequest2 struct { TransType string `json:"trans_type"` Source string `json:"source"` UserID string `json:"user_id"` } func query2(word string) { client := &http.Client{} request := DictRequest2{TransType: "en2zh", Source: word} buf, err := json.Marshal(request) if err != nil { log.Fatal(err) } var data = bytes.NewReader(buf) req, err := http.NewRequest("POST", "https://api.interpreter.caiyunai.com/v1/dict", data) if err != nil { log.Fatal(err) } req.Header.Set("Connection", "keep-alive") req.Header.Set("sec-ch-ua", `" Not A;Brand";v="99", "Chromium";v="8"`) req.Header.Set("sec-ch-ua-mobile", "?0") req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.0.4153 SLBChan/30") req.Header.Set("app-name", "xy") req.Header.Set("Content-Type", "application/json;charset=UTF-8") req.Header.Set("Accept", "application/json, text/plain, */*") req.Header.Set("os-type", "web") req.Header.Set("X-Authorization", "token:qgemv4jr1y38jyq6vhvi") req.Header.Set("Origin", "https://fanyi.caiyunapp.com") req.Header.Set("Sec-Fetch-Site", "cross-site") req.Header.Set("Sec-Fetch-Mode", "cors") req.Header.Set("Sec-Fetch-Dest", "empty") req.Header.Set("Referer", "https://fanyi.caiyunapp.com/") req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9") resp, err := client.Do(req) if err != nil { log.Fatal(err) } defer resp.Body.Close() bodyText, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) } if resp.StatusCode != 200 { log.Fatal("bad StatusCode:", resp.StatusCode, "body", string(bodyText)) } var dictResponse2 DictResponse2 err = json.Unmarshal(bodyText, &dictResponse2) if err != nil { log.Fatal(err) } fmt.Println("----------彩云翻译----------") fmt.Println(word, "UK:", dictResponse2.Dictionary.Prons.En, "US", dictResponse2.Dictionary.Prons.EnUs) for _, item := range dictResponse2.Dictionary.Explanations { fmt.Println(item) } fmt.Println("") } func query(word string) { client := &http.Client{} request := DictRequest{Text: word, Language: "en"} buf, err := json.Marshal(request) if err != nil { log.Fatal(err) } var data = bytes.NewReader(buf) req, err := http.NewRequest("POST", "https://translate.volcengine.com/web/dict/match/v1/?msToken=&X-Bogus=DFSzswVLQDcCwINsSW/NZM9WX7jy&_signature=_02B4Z6wo000015fhpVAAAIDDF-NfE7sRI6-X4aHAAIeXgfG9te8myjutgShQB8jqUDatCy40MOwV-0bqoPUXu9GDqeo.aQKg0Wf1bxIiw8tkAhWWJy3ifEthBdCgN69zoNwnBh2B5hsm-2NKb1", data) if err != nil { log.Fatal(err) } req.Header.Set("authority", "translate.volcengine.com") req.Header.Set("sec-ch-ua", `" Not A;Brand";v="99", "Chromium";v="8"`) req.Header.Set("accept", "application/json, text/plain, */*") req.Header.Set("sec-ch-ua-mobile", "?0") req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.0.4153 SLBChan/30") req.Header.Set("content-type", "application/json") req.Header.Set("origin", "https://translate.volcengine.com") req.Header.Set("sec-fetch-site", "same-origin") req.Header.Set("sec-fetch-mode", "cors") req.Header.Set("sec-fetch-dest", "empty") req.Header.Set("referer", "https://translate.volcengine.com/translate?category=&home_language=zh&source_language=en&target_language=zh&text=hello") req.Header.Set("accept-language", "zh-CN,zh;q=0.9") req.Header.Set("cookie", "x-jupiter-uuid=; i18next=zh-CN; s_v_web_id=verify_1eac8644bba5cc2fca342fbda9d310d2; _tea_utm_cache_2018=undefined; ttcid=0622e7c8207f4922b25f1e3; tt_scid=NKQPTZgJZyxBCL8qyT-pnS71RE74xJ3XV-FO8r7QN04uJ2kj8Q240-UNUSPeo1VN1aff") resp, err := client.Do(req) if err != nil { log.Fatal(err) } defer resp.Body.Close() bodyText, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) } if resp.StatusCode != 200 { log.Fatal("bad StatusCode:", resp.StatusCode, "body", string(bodyText)) } var dictResponse DictResponse err = json.Unmarshal(bodyText, &dictResponse) if err != nil { log.Fatal(err) } fmt.Println("----------火山翻译----------") fmt.Println(word, "UK:", dictResponse.Words[0].PosList[0].Phonetics[0].Text, "US:", dictResponse.Words[0].PosList[0].Phonetics[1].Text) for _, word := range dictResponse.Words { for _, posList := range word.PosList { for _, explanations := range posList.Explanations { fmt.Printf(explanations.Text) } } } fmt.Println("") } func main() { start := time.Now() if len(os.Args) != 2 { fmt.Fprintln(os.Stderr, `usage: simpleDict WORD example: simpleDict hello `) os.Exit(1) } word := os.Args[1] // wg := sync.WaitGroup{} query(word) query2(word) finish := time.Now() fmt.Println("run time:", finish.Sub(start)) }
讯享网
2.运行时间

二、并行后
1.完整代码
讯享网package main import ( "bytes" "encoding/json" "fmt" "io/ioutil" "log" "net/http" "sync" // "strings" "os" "time" ) type DictResponse struct { Words []struct { Source int `json:"source"` Text string `json:"text"` PosList []struct { Type int `json:"type"` Phonetics []struct { Type int `json:"type"` Text string `json:"text"` } `json:"phonetics"` Explanations []struct { Text string `json:"text"` Examples []struct { Type int `json:"type"` Sentences []struct { Text string `json:"text"` TransText string `json:"trans_text"` } `json:"sentences"` } `json:"examples"` Synonyms []interface{} `json:"synonyms"` } `json:"explanations"` Relevancys []interface{} `json:"relevancys"` } `json:"pos_list"` } `json:"words"` Phrases []interface{} `json:"phrases"` BaseResp struct { StatusCode int `json:"status_code"` StatusMessage string `json:"status_message"` } `json:"base_resp"` } type DictRequest struct { Text string `json:"text"` Language string `json:"language"` } type DictResponse2 struct { Rc int `json:"rc"` Wiki struct { KnownInLaguages int `json:"known_in_laguages"` Description struct { Source string `json:"source"` Target interface{} `json:"target"` } `json:"description"` ID string `json:"id"` Item struct { Source string `json:"source"` Target string `json:"target"` } `json:"item"` ImageURL string `json:"image_url"` IsSubject string `json:"is_subject"` Sitelink string `json:"sitelink"` } `json:"wiki"` Dictionary struct { Prons struct { EnUs string `json:"en-us"` En string `json:"en"` } `json:"prons"` Explanations []string `json:"explanations"` Synonym []string `json:"synonym"` Antonym []string `json:"antonym"` WqxExample [][]string `json:"wqx_example"` Entry string `json:"entry"` Type string `json:"type"` Related []interface{} `json:"related"` Source string `json:"source"` } `json:"dictionary"` } type DictRequest2 struct { TransType string `json:"trans_type"` Source string `json:"source"` UserID string `json:"user_id"` } func query2(word string,wg *sync.WaitGroup) { client := &http.Client{} request := DictRequest2{TransType: "en2zh", Source: word} defer wg.Done() buf, err := json.Marshal(request) if err != nil { log.Fatal(err) } var data = bytes.NewReader(buf) req, err := http.NewRequest("POST", "https://api.interpreter.caiyunai.com/v1/dict", data) if err != nil { log.Fatal(err) } req.Header.Set("Connection", "keep-alive") req.Header.Set("sec-ch-ua", `" Not A;Brand";v="99", "Chromium";v="8"`) req.Header.Set("sec-ch-ua-mobile", "?0") req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.0.4153 SLBChan/30") req.Header.Set("app-name", "xy") req.Header.Set("Content-Type", "application/json;charset=UTF-8") req.Header.Set("Accept", "application/json, text/plain, */*") req.Header.Set("os-type", "web") req.Header.Set("X-Authorization", "token:qgemv4jr1y38jyq6vhvi") req.Header.Set("Origin", "https://fanyi.caiyunapp.com") req.Header.Set("Sec-Fetch-Site", "cross-site") req.Header.Set("Sec-Fetch-Mode", "cors") req.Header.Set("Sec-Fetch-Dest", "empty") req.Header.Set("Referer", "https://fanyi.caiyunapp.com/") req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9") resp, err := client.Do(req) if err != nil { log.Fatal(err) } defer resp.Body.Close() bodyText, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) } if resp.StatusCode != 200 { log.Fatal("bad StatusCode:", resp.StatusCode, "body", string(bodyText)) } var dictResponse2 DictResponse2 err = json.Unmarshal(bodyText, &dictResponse2) if err != nil { log.Fatal(err) } fmt.Println("----------彩云翻译----------") fmt.Println(word, "UK:", dictResponse2.Dictionary.Prons.En, "US", dictResponse2.Dictionary.Prons.EnUs) for _, item := range dictResponse2.Dictionary.Explanations { fmt.Println(item) } fmt.Println("") } func query(word string,wg *sync.WaitGroup) { client := &http.Client{} request := DictRequest{Text: word, Language: "en"} defer wg.Done() buf, err := json.Marshal(request) if err != nil { log.Fatal(err) } var data = bytes.NewReader(buf) req, err := http.NewRequest("POST", "https://translate.volcengine.com/web/dict/match/v1/?msToken=&X-Bogus=DFSzswVLQDcCwINsSW/NZM9WX7jy&_signature=_02B4Z6wo000015fhpVAAAIDDF-NfE7sRI6-X4aHAAIeXgfG9te8myjutgShQB8jqUDatCy40MOwV-0bqoPUXu9GDqeo.aQKg0Wf1bxIiw8tkAhWWJy3ifEthBdCgN69zoNwnBh2B5hsm-2NKb1", data) if err != nil { log.Fatal(err) } req.Header.Set("authority", "translate.volcengine.com") req.Header.Set("sec-ch-ua", `" Not A;Brand";v="99", "Chromium";v="8"`) req.Header.Set("accept", "application/json, text/plain, */*") req.Header.Set("sec-ch-ua-mobile", "?0") req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.0.4153 SLBChan/30") req.Header.Set("content-type", "application/json") req.Header.Set("origin", "https://translate.volcengine.com") req.Header.Set("sec-fetch-site", "same-origin") req.Header.Set("sec-fetch-mode", "cors") req.Header.Set("sec-fetch-dest", "empty") req.Header.Set("referer", "https://translate.volcengine.com/translate?category=&home_language=zh&source_language=en&target_language=zh&text=hello") req.Header.Set("accept-language", "zh-CN,zh;q=0.9") req.Header.Set("cookie", "x-jupiter-uuid=; i18next=zh-CN; s_v_web_id=verify_1eac8644bba5cc2fca342fbda9d310d2; _tea_utm_cache_2018=undefined; ttcid=0622e7c8207f4922b25f1e3; tt_scid=NKQPTZgJZyxBCL8qyT-pnS71RE74xJ3XV-FO8r7QN04uJ2kj8Q240-UNUSPeo1VN1aff") resp, err := client.Do(req) if err != nil { log.Fatal(err) } defer resp.Body.Close() bodyText, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) } if resp.StatusCode != 200 { log.Fatal("bad StatusCode:", resp.StatusCode, "body", string(bodyText)) } var dictResponse DictResponse err = json.Unmarshal(bodyText, &dictResponse) if err != nil { log.Fatal(err) } fmt.Println("----------火山翻译----------") fmt.Println(word, "UK:", dictResponse.Words[0].PosList[0].Phonetics[0].Text, "US:", dictResponse.Words[0].PosList[0].Phonetics[1].Text) for _, word := range dictResponse.Words { for _, posList := range word.PosList { for _, explanations := range posList.Explanations { fmt.Printf(explanations.Text) } } } fmt.Println("") } func main() { start := time.Now() if len(os.Args) != 2 { fmt.Fprintln(os.Stderr, `usage: simpleDict WORD example: simpleDict hello `) os.Exit(1) } word := os.Args[1] var wg sync.WaitGroup // wg := sync.WaitGroup{} wg.Add(2) go query(word, &wg) go query2(word, &wg) wg.Wait() finish := time.Now() fmt.Println("run time:",finish.Sub(start)) }
2.运行时间

明显并行后的运行时间更快

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/106352.html