Go语言实践作业第六篇| 豆包MarsCode AI刷题

107 阅读4分钟

程序的主要组成部分如下:

  1. 结构体定义

    • DictRequest:用于定义发送到API的请求数据结构,包含TransType(翻译类型)、Source(源单词)和UserID(用户ID)。
    • DictResponse:用于定义从API接收的响应数据结构,包含字典信息和维基百科信息。
  2. query函数

    • 这个函数接受一个单词作为参数,构造一个HTTP POST请求,并将请求发送到API。
    • 使用json.MarshalDictRequest结构体序列化为JSON格式。
    • 设置请求头,包括Content-TypeUser-AgentX-Authorization等,这些是API识别请求来源和验证请求合法性的关键信息。
    • 发送请求并接收响应,检查状态码是否为200(OK),如果不是,则记录错误并退出。
    • 解析响应体中的JSON数据到DictResponse结构体。
    • 打印出单词的英式和美式发音,以及所有的解释。
  3. main函数

    • 程序的入口点,检查命令行参数的数量是否正确(需要一个单词作为参数)。
    • 调用query函数并传入命令行参数作为单词进行查询。
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)
	}
}

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

该程序实现了一个简单的命令行查字母的工具。命令行接受一个参数:需查询的单词。运行程序即可输出该单词发音与释义。

这段代码体现了Go语言的几个特色和编程习惯:

  1. 类型系统和结构体

    • Go语言强类型系统的特点体现在定义了两个结构体DictRequestDictResponse来匹配JSON请求和响应的数据结构。这使得代码类型安全,并且可以在编译时检查错误。
  2. 接口和类型断言

    • DictResponse结构体中,Target字段被定义为interface{}类型,这是因为JSON中的target字段可能是不同的数据类型。Go的接口类型允许这种灵活性,但在使用时需要进行类型断言。
  3. 错误处理

    • Go语言中的error类型被广泛用于错误处理。在这段代码中,每个可能失败的操作(如JSON序列化、HTTP请求等)都返回了一个error类型的值,并且通过if err != nil进行了检查。
  4. 并发和网络编程

    • Go语言对并发和网络编程提供了良好的支持。虽然这段代码没有直接使用goroutine或channel,但它展示了Go语言在网络请求方面的简洁性,例如使用http.Client发送请求。
  5. 标准库的使用

    • 代码中使用了Go的标准库,如encoding/json进行JSON的序列化和反序列化,net/http进行HTTP请求的发送和接收,io/ioutil用于读取响应体等。
  6. 简洁的语法

    • Go语言的语法简洁,例如使用:=进行短变量声明,使用log.Fatal在错误发生时直接退出程序,这些都是Go语言特有的简洁语法。
  7. 命令行参数处理

    • 使用os.Args来处理命令行参数,这是Go语言处理命令行参数的标准方式。
  8. defer语句

    • 使用defer关键字来确保在函数返回之前关闭响应体,这是一种资源清理的常用做法,可以防止资源泄露。
  9. 字符串连接和格式化

    • 使用fmt.Printffmt.Println进行字符串的格式化输出,这是Go语言中常用的输出方式。
  10. 代码组织

    • 代码按照功能划分为query函数和main函数,体现了Go语言模块化和函数式编程的风格。

这段代码虽然没有使用到Go语言的所有特色,但它展示了Go在网络编程、错误处理、类型系统和标准库使用方面的强大能力。总的来说是一个对新手很不错的实践作业学习,我通过学习这次作业收获匪浅。