Go语言第一课 | 青训营笔记

152 阅读4分钟

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

因为实战2和实战3对我来说比较难,所以记录一下自己跟着老师进行操作的过程。

实战2 在线词典-代码生成

1. 打开网址-彩云小译

https://fanyi.caiyunapp.com/

自己的截图

image.png

image.png

image.png curl as cURL (bash),这个和老师那个才是一样的。

curl 'https://api.interpreter.caiyunai.com/v1/dict' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'Accept-Language: zh-CN,zh;q=0.9' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/json;charset=UTF-8' \
  -H 'Origin: https://fanyi.caiyunapp.com' \
  -H 'Referer: https://fanyi.caiyunapp.com/' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Site: cross-site' \
  -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36' \
  -H 'X-Authorization: token:qgemv4jr1y38jyq6vhvi' \
  -H 'app-name: xy' \
  -H 'device-id: ' \
  -H 'os-type: web' \
  -H 'os-version: ' \
  -H 'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "Windows"' \
  --data-raw '{"trans_type":"en2zh","source":"good"}' \
  --compressed

2.打开网址-curlconverter

https://curlconverter.com/#go

这个是用curl as cURL(bash)的结果,和老师的一样

image.png

package main
​
import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "strings"
)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("Accept", "application/json, text/plain, */*")
    req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")
    req.Header.Set("Connection", "keep-alive")
    req.Header.Set("Content-Type", "application/json;charset=UTF-8")
    req.Header.Set("Origin", "https://fanyi.caiyunapp.com")
    req.Header.Set("Referer", "https://fanyi.caiyunapp.com/")
    req.Header.Set("Sec-Fetch-Dest", "empty")
    req.Header.Set("Sec-Fetch-Mode", "cors")
    req.Header.Set("Sec-Fetch-Site", "cross-site")
    req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36")
    req.Header.Set("X-Authorization", "token:qgemv4jr1y38jyq6vhvi")
    req.Header.Set("app-name", "xy")
    req.Header.Set("os-type", "web")
    req.Header.Set("sec-ch-ua", `" Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"`)
    req.Header.Set("sec-ch-ua-mobile", "?0")
    req.Header.Set("sec-ch-ua-platform", `"Windows"`)
    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)
}

放到goland运行结果,会返回一堆json

image.png

修改代码,加上

type DicRequest struct{}以及main里面也要改一些

image.png

修改之后的运行结果应该和原来的差不多v2

image.png

3.打开网站OKTools

https://oktools.net/json2go

image.png

复制粘贴preview里面的json字符串

{
  "rc": 0,
  "wiki": {
    "known_in_laguages": 63,
    "description": {
      "source": "tangible and intangible thing, except labor tied services, that satisfies human wants and provides utility",
      "target": null
    },
    "id": "Q28877",
    "item": {
      "source": "good",
      "target": "商品"
    },
    "image_url": "http://www.caiyunapp.com/imgs/link_default_img.png",
    "is_subject": "true",
    "sitelink": "https://www.caiyunapp.com/read_mode/?id=625b4b949c0120504d1e7b69"
  },
  "dictionary": {
    "prons": {
      "en-us": "[gʊd]",
      "en": "[gud]"
    },
    "explanations": [
      "a.好的;善良的;快乐的;真正的;宽大的;有益的;老练的;幸福的;忠实的;优秀的;完整的;彻底的;丰富的",
      "n.利益;好处;善良;好人",
      "ad.=well"
    ],
    "synonym": [
      "excellent",
      "fine",
      "nice",
      "splendid",
      "proper"
    ],
    "antonym": [
      "bad",
      "wrong",
      "evil",
      "harmful",
      "poor"
    ],
    "wqx_example": [
      [
        "to the good",
        "有利,有好处"
      ],
      [
        "good, bad and indifferent",
        "好的,坏的和一般的"
      ],
      [
        "good innings",
        "长寿"
      ],
      [
        "good and ...",
        "很,颇;完全,彻底"
      ],
      [
        "do somebody's heart good",
        "对某人的心脏有益,使某人感到愉快"
      ],
      [
        "do somebody good",
        "对某人有益"
      ],
      [
        "be good for",
        "对…有效,适合,胜任"
      ],
      [
        "be good at",
        "在…方面(学得,做得)好;善于"
      ],
      [
        "as good as one's word",
        "信守诺言,值得信赖"
      ],
      [
        "as good as",
        "实际上,几乎等于"
      ],
      [
        "all well and good",
        "也好,还好,很不错"
      ],
      [
        "a good",
        "相当,足足"
      ],
      [
        "He is good at figures . ",
        "他善于计算。"
      ]
    ],
    "entry": "good",
    "type": "word",
    "related": [],
    "source": "wenquxing"
  }
}

image.png

image.png

点击转换-嵌套结果

 //后面将AutoGenerated改个名字为DicResponse
type AutoGenerated 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"`
}

修改一些代码,v3

image.png

运行结果

image.png

修改代码

image.png

image.png

要在终端输入go run main.go hello

不然直接运行就会这样

image.png

实战3 SOCKS5代理介绍

1.TCP echo server-v1

NETCAT安装配置配置好环境变量

https://blog.csdn.net/qq_40359932/article/details/105840295

先输入

image.png

nc 127.0.0.1 1080
再输入
hello

服务器返回hello

image.png

2.auth 认证阶段 -v2

curl命令

curl --socks5 127.0.0.1:1080 -v http://www.qq.com

image.png

我之前已经安装过curl,版本如下,在cmd里面可以用。 image.png

但是不知道为什么在goland里面会报错

image.png

有日志,支持0和1说明是正确的

image.png

所以最终打算先将就着goland和cmd配合使用

image.png

3.请求阶段-v3

输出结果

image.png

4.relay阶段-v4

image.png

5. 浏览器配置代理

安装插件

点击新建情景模式,按照下面配置,点击应用选项,点圈圈选择代理

image.png

之后,我们在浏览器上面访问网页,在终端会输入我们访问的域名等信息

例如

image.png

终端输出

image.png

课后总结

这是青训营的第一堂课,前面关于go的基础语法很快地就过了一遍,在讲到指针的那一节的时候,已经有一点听不懂,所以在课后要自己补一下这个语法。实战2和实战3在第一次听的时候没跟上,但是后来有慢慢地跟着一步步做,又听了几遍,其中有遇到了一些问题,不过最终都解决了。