Go语言快速入门 | 青训营笔记

121 阅读4分钟

这是我参与「第三届青训营 -后端场」笔记创作活动的的第1篇笔记 第一次课程前半部分讲了Go基本语法,后半部分讲了三个小项目

零、Go基础

1、基本语法

  • 变量

    声明、初始化、匿名变量

  • 数据类型

    整形、浮点型、布尔、字符型、字符串、切片、

  • 指针

  • 变量的生命周期

  • 常量

  • 变量别名

2、容器

  • 数组

    声明、初始化、遍历

  • 切片(slice)

    make()、append()

  • 映射(map)

  • 列表(list)

3、流程控制

  • 条件判断:if
  • 循环:for
  • 键值循环:for range
  • 分支选择:switch
  • 代码跳转:goto

一、猜字谜游戏

1、代码版本一

package main
​
import (
  "fmt"
  "math/rand"
)
​
func main() {
  maxNum := 100
  secretNumber := rand.Intn(maxNum)
  fmt.Println("The secret number is ", secretNumber)
}

每次运行输出的数字都是同样的

随机数种子

2、代码版本二

需要设置代码随机数种子,设置为当前的时间戳

rand.Seed(time.Now().UnixNano())

3、代码版本三

实现用户的输入输出

reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
  fmt.Println("An error occured while reading input. Please try again", err)
  return
}
input = strings.TrimSuffix(input, "\n")

4、代码版本四

使用循环实现游戏的循环

变量、循环、函数、文件流、错误处理等

二、命令行版本词典

调用第三方API

使用Go发送网络请求,解析JSON,并使用相应的代码生成工具提高开发效率

1、抓包

抓取第三方翻译平台的API

彩云翻译

按f12打开开发人员工具,找到正确的请求

2、代码生成

右键请求

复制结果如下:

curl 'https://api.interpreter.caiyunai.com/v1/dict' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7' \
  -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 (Macintosh; Intel Mac OS X 10_15_7) 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: "macOS"' \
  --data-raw '{"trans_type":"en2zh","source":"good"}' \
  --compressed

然后我们就去这个平台生成代码:Go代码生成

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", "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7")
  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 (Macintosh; Intel Mac OS X 10_15_7) 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", `"macOS"`)
  // 发起请求
  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)
}

3、生成requst body

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

将请求结构体序列化为JSON:

request := DictRequest{TransType: "en2zh", Source: "good"}
buf, err := json.Marshal(request)

4、response body的解析

由于结构复杂,为了防止出错,我们使用第三方代码生成工具

去这个平台进行Go结构体的生成:Go结构体

type AutoGenerated struct {
  Rc int `json:"rc"`
  Wiki Wiki `json:"wiki"`
  Dictionary Dictionary `json:"dictionary"`
}
type Description struct {
  Source string `json:"source"`
  Target interface{} `json:"target"`
}
type Item struct {
  Source string `json:"source"`
  Target string `json:"target"`
}
type Wiki struct {
  KnownInLaguages int `json:"known_in_laguages"`
  Description Description `json:"description"`
  ID string `json:"id"`
  Item Item `json:"item"`
  ImageURL string `json:"image_url"`
  IsSubject string `json:"is_subject"`
  Sitelink string `json:"sitelink"`
}
type Prons struct {
  EnUs string `json:"en-us"`
  En string `json:"en"`
}
type Dictionary struct {
  Prons Prons `json:"prons"`
  Explanations []string `json:"explanations"`
  Synonym []string `json:"synonym"`
  Antonym []string `json:"antonym"`
  WqxExample []WqxExample[]string `json:"wqx_example"`
  Entry string `json:"entry"`
  Type string `json:"type"`
  Related []interface{} `json:"related"`
  Source string `json:"source"`
}

接下来就反序列化response body了:

var dictResponse DictResponse
err = json.Unmarshal(bodyText, &dictResponse)

获取JSON中指定的信息

fmt.Println("UK:", dictResponse.Dictionary.Prons.En, "US:", dictResponse.Dictionary.Prons.EnUs)

5、获取命令行参数

if len(os.Args) != 2 {
  fmt.Fprintf(os.Stderr, `usage: simpleDict WORD
example: simpleDict hello
    `)
  os.Exit(1)
}
word := os.Args[1]

三、SOCKET5代理服务器

1、socks5原理

2、向一个连接读写数据

读数据:

reader := bufio.NewReader(conn)
b, err := reader.ReadByte()

写数据:

_, err = conn.Write([]byte{b})

4、连接的各个流程

基于字节流,根据协议规则,依次读取字节,将字节拼接成不同的具体数据

5、实现双向的数据转发

任何一个方向的数据copy失败,我们就返回这个函数,并关闭连接清理数据

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
  _, _ = io.Copy(dest, reader)
  cancel()
}()
go func() {
  _, _ = io.Copy(conn, dest)
  cancel()
}()
<-ctx.Done()