这是一个在线字典demo,主要是考察go语言的网络请求响应,还有一些基本的数据类型操作。
一、效果展示
使用命令行工具输入go run dictionary.go <word>程序会将word的中文释义打印出来,如下所示:
$ go run dictionary.go hello
hello UK: [ˈheˈləu] US: [həˈlo]
int.喂;哈罗
n.引人注意的呼声
v.向人呼(喂
二、代码实现
2.1 引入依赖
首先我们定义好package和需要import的依赖
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
)
2.2 定义结构体
因为我们需要向在线的api发送请求并解析响应,这里的请求响应都是json格式数据,那我们需要定义相对应的结构体方便我们在程序中将数据解码或编码成为对应的json格式。这里我们定义请求和响应的结构题如下:
type DictRequest struct {
TransType string `json:"trans_type"`
Source string `json:"source"`
UserID string `json:"user_id"`
}
type DictResponse struct {
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"`
}
2.3 编写查询函数
这里我们编写一个函数用来发送请求,这个函数需要接受一个string类型的参数作为查询的单词,这个参数需要被编码成json数据格式形成请求报文然后发送给在线翻译的api。 这里我们编写代码如下:
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.NewBuffer(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")
req.Header.Set("app-name", "xy")
req.Header.Set("content-type", "application/json;charset=UTF-8")
req.Header.Set("device-id", "df4f7b22a8de96b304cbfc4623f77b51")
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", `"Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"`)
req.Header.Set("sec-ch-ua-mobile", "?0")
req.Header.Set("sec-ch-ua-platform", `"macOS"`)
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/113.0.0.0 Safari/537.36")
req.Header.Set("x-authorization", "token:qgemv4jr1y38jyq6vhvi")
}
这里的req就是已经编码完成的报文,下面我们在函数的后面添加发送和处理响应的代码:
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bodyText, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
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)
}
这样我们的query函数就完成了,我们的发送请求和处理响应并打印的逻辑都放在这个函数中实现。
2.3 main函数
最后我们实现一下main函数,主要作用是把要查询的单词通过命令行参数的形式传进来:
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: dictionary <word>")
os.Exit(1)
}
word := os.Args[1]
query(word)
}
最后保存,在命令行中运行一下代码,是可以正常打印相关单词的释义的。总体来说这个需求还是很简单的,主要关注一下代码中关于网络通信部分的操作:包括创建clinet对象,创建和操作request对象,还有buffer的读取等等。