02 Go 语言原理和基础实战(中)| 青训营笔记

176 阅读3分钟

这是我参与「第五届青训营」伴学笔记创作活动的第二天。
这一篇是之前学习过的课程笔记总结。记录自己学习过程。

知识要点

  1. 实战案例
    • 猜谜游戏
    • 在线字典

知识点详解

猜谜游戏

程序要求:程序每次产生一个0-100的数字,玩家进行猜测;程序会告诉玩家是否高于或者低于,直到猜测正确。
分析 这问题比较容易,只需要使用for循环,条件判断和random随机即可。
注意输入和输出的操作。
大致流程图如下所示:

graph TD 
    start([开始]) -->op1[生成随机数]  
    op1 --> input[输入猜测数] 
    input --> |输入形式不正确| input
    input --> cond{判断数字正确}
    cond -->|正确| output1[猜测正确]   
    cond --> |偏大| output2[猜测偏大]
    cond --> |偏小| output3[猜测偏小]
    output1 --> ending([结束])
    output2 --> cont[请继续输入]
    output3 --> cont
    cont --> input

代码

package main

import (
	"bufio"
	"errors"
	"fmt"
	"math/rand"
	"os"
	"strconv"
	"strings"
	"time"
)

// 读入数据
func input_func() (guess int, err error) {
	reader := bufio.NewReader(os.Stdin)
	input, err1 := reader.ReadString('\n')
	if err1 != nil {
		fmt.Println("An error occured during input, please try again", err)
		return -1, errors.New("wrong input")
	}

	input = strings.TrimSuffix(input, "\n")
	input = strings.TrimSuffix(input, "\r")

	guess, err2 := strconv.Atoi(input)
	if err2 != nil {
		fmt.Println("Invalid input, please input an interger")
		return -1, errors.New("invalid input")
	}
	return guess, nil
}

func main() {
	// 生成随机数 (注意随机种子)
	MAX_NUM := 100
	rand.NewSource(time.Now().UnixNano())
	secretNum := rand.Intn(MAX_NUM)
	fmt.Println("We have generated the secret number.")
	fmt.Println("Please input your guess: ")

	// 死循环保持游戏持续
	for {
		//输入
		guess, err := input_func()

		if err != nil {
			continue
		}
		// 逻辑判断
		if guess > secretNum {
			fmt.Println("Your guess is bigger than the secret number. Try again")
		} else if guess < secretNum {
			fmt.Println("Your guess is small than the secret number. Try again")
		} else {
			fmt.Println("Your guess is Correct!")
			break
		}
	}
}

在线字典

程序要求:一个命令行词典,在命令行内输入单词,然后查找音标和注释(通过三方API)
准备 使用API为彩云科技 fanyi.caiyunapp.com

查找该POST请求以及对应的json字段 image.png

由于发送该请求比较麻烦,我们使用代码生成的方式来生成该请求,选择方式如下(这里我们以Microsoft Edge为例子):

c48198399feb22073856da60097d97d.png

然后打开网站Convert curl to Go (curlconverter.com):粘贴得到对应代码如下

image.png 大致代码请求过程如下所示:

graph LR 
    start([开始]) -->op1[创建请求]
    op1 --> op2[设置请求头]
    op2 --> op3[发起请求]
    op3 --> op4[读取响应]
    op4 --> ending([结束])

将对应代码粘贴后,运行得到如下结果:

image.png 后面需要更改输入,只需要构造结构体,进行JSON 序列化。
我们发现只需要构造的字段有 trans_type, source 即可,所以将原始代码构造部分修改如下即可:

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


//.....
//.....

//开始构造data
request := DictRequest{TransType: "en2zh", Source: "nice"}
buf, err := json.Marshal(request)

if err != nil {
        log.Fatal(err)
}
var data = bytes.NewReader(buf)

最后,我们需要添加解析 respose body代码,使得正确显示(反序列化):
一种常用方式是通过结构体定义,将JSON数据反序列化到结构体中即可。
由于结构体比较难定义,这里我们使用JSON转Golang Struct - 在线工具 - OKTools来生成对应代码段=> 将我们Preview(预览)部分的结构体进行粘贴,得到如下结果:
因为不需要过多操作,我们使用嵌套方式转换

type AutoGenerated struct {
	Rc int `json:"rc"`
	Wiki struct {
	} `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 dictRespose DictRespose
err = json.Unmarshal(bodyText, &dictRespose)
if err != nil {
        log.Fatal(err)
}
fmt.Printf("%#v\n", dictRespose)

将输入进行修改,并且添加最后打印的必要字段,我们可以得到最后整体代码,如下所示:

package main

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

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

type DictRespose struct {
	Rc   int `json:"rc"`
	Wiki struct {
	} `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{}

	//开始构造data
	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("authority", "api.interpreter.caiyunai.com")
	req.Header.Set("accept", "application/json, text/plain, */*")
	req.Header.Set("accept-language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
	req.Header.Set("app-name", "xy")
	req.Header.Set("content-type", "application/json;charset=UTF-8")
	req.Header.Set("device-id", "")
	req.Header.Set("origin", "https://fanyi.caiyunapp.com")
	req.Header.Set("os-type", "web")
	req.Header.Set("os-version", "")
	req.Header.Set("referer", "https://fanyi.caiyunapp.com/")
	req.Header.Set("sec-ch-ua", `"Chromium";v="110", "Not A(Brand";v="24", "Microsoft Edge";v="110"`)
	req.Header.Set("sec-ch-ua-mobile", "?0")
	req.Header.Set("sec-ch-ua-platform", `"Windows"`)
	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/110.0.0.0 Safari/537.36 Edg/110.0.1587.41")
	req.Header.Set("x-authorization", "token:qgemv4jr1y38jyq6vhvi")
	// 发起请求
	resp, err := client.Do(req)

	if err != nil {
		log.Fatal(err)
	}

	defer resp.Body.Close() // 返回resp body 为流,需要手动关闭流(防止资源泄露)

	// 读取响应
	bodyText, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	// 防御式编程,防止编程出错
	if resp.StatusCode != 200 {
		log.Fatal("bad StatusCode", resp.StatusCode, "body", string(bodyText))
	}
	// 反序列化显示
	var dictRespose DictRespose
	err = json.Unmarshal(bodyText, &dictRespose)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(word, "UK:", dictRespose.Dictionary.Prons.En, "US:", dictRespose.Dictionary.Prons.EnUs)
	for _, item := range dictRespose.Dictionary.Explanations {
		fmt.Println(item)
	}
}

func main() {
	if len(os.Args) != 2 {
		fmt.Fprintf(os.Stderr, `usage: simple_dict WORD \nexample: simple_dict hello`)
		os.Exit(1)
	}
	word := os.Args[1]
	query(word)
}

最后编译运行结果如下所示:

image.png

参考资料和补充阅读

  1. 课程地址:juejin.cn/course/byte…
  2. 课程源代码:github.com/wangkechun/…
  3. 课程参考书籍:前言 · Go语言圣经 (studygolang.com)