笔记&作业<第一节> | 青训营笔记

391 阅读7分钟

这是我参与「第三届青训营 -后端场」笔记创作活动的第1篇笔记

引言

本文为青训营第一节课的课程笔记以及作业,第一节课的主要内容如下:

  1. Go 语言简介
  2. Go 语言开发入门,包括开发环境配置、基础语法、标准库
  3. Go 实战,包括三个实战项目,分别是猜数字、在线词典、socks5代理

笔记

1. 猜数字

//这是伪随机数
maxNum := 100
secretNumber := rand.Intn(maxNum) 

//正确做法
maxNum := 100
rand.Seed(time.Now().UnixNano())
secretNumber := rand.Intn(maxNum)

2. 在线词典

2.1 在线词典-抓包

右键 --> 检查 --> 选中Network --> 选中ALl --> 选中dict --> 确认Header、Payload等内容是我们需要的内容。

image.png

2.2 在线词典-代码生成

在线生成工具:curlconverter.com/#go

  1. 选中请求,copy as curl

image.png

  1. 粘贴后即刻转换

image.png

  1. 代码如下:
func main() {
   client := &http.Client{}
   var data = strings.NewReader(`{"trans_type":"en2zh","source":"good"}`)
   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("DNT", "1")
   req.Header.Set("os-version", "")
   req.Header.Set("sec-ch-ua-mobile", "?0")
   req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36")
   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("device-id", "")
   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")
   req.Header.Set("Cookie", "_ym_uid=16456948721020430059; _ym_d=1645694872")
   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)
   }
   fmt.Printf("%s\n", bodyText)
}

2.3 在线词典-生成 request body

在线转换工具:oktools.net/json2go

  1. 复制response里的json

image.png

  1. 使用转换工具嵌套转换

image.png

  1. 修改代码使request能传入字符串参数
type DictRequest struct {
   TransType string `json:"trans_type"`
   Source    string `json:"source"`
   UserID    string `json:"user_id"`
}

func query(word string) {
   client := &http.Client{}
   request := DictRequest{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)
   }
   ......
}

2.4 在线词典-解析 response body

  1. 复制response里的json

image.png

  1. 使用转换工具嵌套转换 image.png

  2. 修改代码使response能便于处理

type DictResponse 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"`
}

......

var dictResponse DictResponse
err = json.Unmarshal(bodyText, &dictResponse)
if err != nil {
   log.Fatal(err)
}
fmt.Println(word, "UK:", dictResponse.Dictionary.Prons.En, "US:", dictResponse.Dictionary.Prons.EnUs)
for _, item := range dictResponse.Dictionary.Explanations {
   fmt.Println(item)
}

2.5 在线词典-完整代码,打印输出

package main

import (
   "bytes"
   "encoding/json"
   "fmt"
   "io/ioutil"
   "log"
   "net/http"
   "os"
)

type DictRequest struct {
   TransType string `json:"trans_type"`
   Source    string `json:"source"`
   UserID    string `json:"user_id"`
}

type DictResponse 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"`
}

func query(word string) {
   client := &http.Client{}
   request := DictRequest{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("DNT", "1")
   req.Header.Set("os-version", "")
   req.Header.Set("sec-ch-ua-mobile", "?0")
   req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36")
   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("device-id", "")
   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")
   req.Header.Set("Cookie", "_ym_uid=16456948721020430059; _ym_d=1645694872")
   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(word, "UK:", dictResponse.Dictionary.Prons.En, "US:", dictResponse.Dictionary.Prons.EnUs)
   for _, item := range dictResponse.Dictionary.Explanations {
      fmt.Println(item)
   }
}

//go run simpledict/v4/main.go hello
func main() {
   if len(os.Args) != 2 {
      fmt.Fprintf(os.Stderr, `usage: simpleDict WORD
example: simpleDict hello
      `)
      os.Exit(1)
   }
   word := os.Args[1]
   query(word)
}

运行:go run simpledict/v4/main.go hello

image.png

3. socks5代理

参考我的下一篇笔记:SOCKS5 代理实现|青训营笔记 juejin.cn/post/709505…

作业

  1. 修改第一个例子猜谜游戏里面的最终代码,使用 fmt.Scanf 来简化代码实现
/*
* @Author: starine
* @Date:   2022/5/6 13:37
* 修改第一个例子猜谜游戏里面的最终代码,使用 fmt.Scanf 来简化代码实现
 */

package main

import (
   "fmt"
   "math/rand"
   "time"
)

func main() {
   maxNum := 100
   rand.Seed(time.Now().UnixNano())
   secretNumber := rand.Intn(maxNum)

   var guess int
   fmt.Println("Please input your guess")
   for {
      _, err := fmt.Scanf("%d", &guess)
      if err != nil {
         fmt.Printf("Invalid input: %v. Please enter an integer value\n", err)
         continue
      }
      fmt.Println("You guess is", guess)
      if guess > secretNumber {
         fmt.Println("Your guess is bigger than the secret number. Please try again")
      } else if guess < secretNumber {
         fmt.Println("Your guess is smaller than the secret number. Please try again")
      } else {
         fmt.Println("Correct, you Legend!")
         break
      }
   }
}
  1. 修改第二个例子命令行词典里面的最终代码,增加另一种翻译引擎的支持
  • 尝试增加了DeepL翻译引擎,但是返回的结果是Too many requests:
$ go run simpledict/v5/main.go hello
{"jsonrpc":"2.0","error":{"code":1042911,"message":"Too many requests"}}

请大佬来解决一下,代码如下:

/*
* @Author: starine
* @Date:   2022/5/6 16:20
 */

package main

import (
   "bytes"
   "encoding/json"
   "fmt"
   "io/ioutil"
   "log"
   "math/rand"
   "net/http"
   "os"
   "time"
)
type DeepLRequest struct {
   Jsonrpc string `json:"jsonrpc"`
   Method string `json:"method"`
   Params Params `json:"params"`
   ID int `json:"id"`
}
type Sentences struct {
   Text string `json:"text"`
   ID int `json:"id"`
   Prefix string `json:"prefix"`
}
type Jobs struct {
   Kind string `json:"kind"`
   Sentences []Sentences `json:"sentences"`
   RawEnContextBefore []interface{} `json:"raw_en_context_before"`
   RawEnContextAfter []interface{} `json:"raw_en_context_after"`
   PreferredNumBeams int `json:"preferred_num_beams"`
   Quality string `json:"quality"`
}
type Weight struct {
   DE float64 `json:"DE"`
   EN float64 `json:"EN"`
   ES float64 `json:"ES"`
   FR float64 `json:"FR"`
   IT float64 `json:"IT"`
   JA float64 `json:"JA"`
   NL float64 `json:"NL"`
   PL float64 `json:"PL"`
   PT float64 `json:"PT"`
   RU float64 `json:"RU"`
   ZH float64 `json:"ZH"`
   BG float64 `json:"BG"`
   CS float64 `json:"CS"`
   DA float64 `json:"DA"`
   EL float64 `json:"EL"`
   ET float64 `json:"ET"`
   FI float64 `json:"FI"`
   HU float64 `json:"HU"`
   LT float64 `json:"LT"`
   LV float64 `json:"LV"`
   RO float64 `json:"RO"`
   SK float64 `json:"SK"`
   SL float64 `json:"SL"`
   SV float64 `json:"SV"`
}
type Preference struct {
   Weight Weight `json:"weight"`
   Default string `json:"default"`
}
type Lang struct {
   Preference Preference `json:"preference"`
   SourceLangUserSelected string `json:"source_lang_user_selected"`
   TargetLang string `json:"target_lang"`
}
type CommonJobParams struct {
   BrowserType int `json:"browserType"`
   Formality interface{} `json:"formality"`
}
type Params struct {
   Jobs []Jobs `json:"jobs"`
   Lang Lang `json:"lang"`
   Priority int `json:"priority"`
   CommonJobParams CommonJobParams `json:"commonJobParams"`
   Timestamp int64 `json:"timestamp"`
}

func queryDeepL(word string) {
   client := &http.Client{}
   var jsonBlob = []byte(`{"jsonrpc":"2.0","method": "LMT_handle_jobs","params":{"jobs":[],"lang":{"preference":{"weight":{"DE":0.2019,"EN":0.43331,"ES":0.12247,"FR":0.1572,"IT":0.0622,"JA":0.04917,"NL":0.04569,"PL":0.03126,"PT":0.02817,"RU":0.02017,"ZH":0.39912,"BG":0.00537,"CS":0.01616,"DA":0.01985,"EL":0.00339,"ET":0.01623,"FI":0.02824,"HU":0.01326,"LT":0.01068,"LV":0.0109,"RO":0.01179,"SK":0.01337,"SL":0.01025,"SV":0.01859},"default":"default"},"source_lang_user_selected":"auto","target_lang":"ZH"},"priority":-1,"commonJobParams":{"browserType":1,"formality":null},"timestamp":1651833661400},"id":0}`)
   var deepLRequest DeepLRequest
   err := json.Unmarshal(jsonBlob, &deepLRequest)
   if err != nil {
      log.Fatal(err)
   }
   deepLRequest.Params.Jobs = []Jobs{{
      Kind:"default",
      Sentences: []Sentences{{word,0,""}},
      RawEnContextBefore:nil,
      RawEnContextAfter:nil,
      PreferredNumBeams:4,
      Quality:"fast",
   }}
   deepLRequest.Params.Timestamp = time.Now().Unix()
   rand.Seed(time.Now().UnixNano())
   deepLRequest.ID = int(50000000 + rand.Intn(10000000))
   buf, err := json.Marshal(deepLRequest)
   if err != nil {
      log.Fatal(err)
   }
   var data = bytes.NewReader(buf)
   req, err := http.NewRequest("POST", "https://www2.deepl.com/jsonrpc?method=LMT_handle_jobs", data)
   if err != nil {
      log.Fatal(err)
   }
   req.Header.Set("authority", "www2.deepl.com")
   req.Header.Set("accept", "*/*")
   req.Header.Set("accept-language", "zh-CN,zh;q=0.9")
   req.Header.Set("content-type", "application/json")
   req.Header.Set("cookie", "dapUid=ee20bc91-a4af-45c1-a9ef-72c0c025fa8a; privacySettings=%7B%22v%22%3A%221%22%2C%22t%22%3A1651795200%2C%22m%22%3A%22LAX_AUTO%22%2C%22consent%22%3A%5B%22NECESSARY%22%2C%22PERFORMANCE%22%2C%22COMFORT%22%5D%7D; LMTBID=v2|300d236a-191a-464a-b184-bcfff23e7a31|0caa57b397dab113b3767b710e169a3b; dapVn=2; __cf_bm=PaQJLomrOevyk1qfIjAVcarJ9oVGmvsIP1YYhSYKNOY-1651833349-0-AaooAj7OYY5l/P0EA/Lg3lZEXg45Fu6/Wkw8vdtLbdGu97Dgr5c6jRnDmhB4o58dgWahyofF2GMxkPabgkqq40M=; dapSid=%7B%22sid%22%3A%22f6c9f916-b20e-49f6-8f29-6b256bcf63c5%22%2C%22lastUpdate%22%3A1651833661%7D")
   req.Header.Set("origin", "https://www.deepl.com")
   req.Header.Set("referer", "https://www.deepl.com/")
   req.Header.Set("sec-ch-ua", `" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"`)
   req.Header.Set("sec-ch-ua-mobile", "?1")
   req.Header.Set("sec-ch-ua-platform", `"Android"`)
   req.Header.Set("sec-fetch-dest", "empty")
   req.Header.Set("sec-fetch-mode", "cors")
   req.Header.Set("sec-fetch-site", "same-site")
   req.Header.Set("user-agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Mobile Safari/537.36")
   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)
   }
   fmt.Printf("%s\n", bodyText)
}


//go run simpledict/v5/main.go hello
func main() {
   if len(os.Args) != 2 {
      fmt.Fprintf(os.Stderr, `usage: simpleDict WORD
example: simpleDict hello
      `)
      os.Exit(1)
   }
   word := os.Args[1]
   queryDeepL(word)
}
  1. 在上一步骤的基础上,修改代码实现并行请求两个翻译引擎来提高响应速度

DeepL翻译不支持之后,换成了知网翻译,但是我只想说:天下苦知网久已!

WechatIMG318副本.png

并行核心代码:

var wg = sync.WaitGroup{}
func main() {
   if len(os.Args) != 2 {
      fmt.Fprintf(os.Stderr, `usage: simpleDict WORD
example: simpleDict hello
      `)
      os.Exit(1)
   }
   word := os.Args[1]
   wg.Add(1)
   go ZhiWangQuery(word)
   wg.Add(1)
   go CaiYunQuery(word)
   wg.Wait()
}