DAY.1三个例子自己的理解

48 阅读2分钟

# 1字典问题要注意headler中不要有自己得到响应时所用的参数 `// +bulid ignore package main

import ( "bytes" "encoding/json" "fmt" "io" "log" "net/http" "os" //"strings" ) 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 { } 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 []interface{} 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{} //var data = strings.NewReader({"trans_type":"en2zh","source":"hello"}) 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)
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.183")

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", "en-GB,en;q=0.9,zh-CN;q=0.8,zh;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", "5790c2094fb3036d9c5358e89c46d4a8")
req.Header.Set("dnt", "1")
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", `"Not/A)Brand";v="99", "Microsoft Edge";v="115", "Chromium";v="115"`)
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;hello x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.183")
req.Header.Set("x-authorization", "token:qgemv4jr1y38jyq6vhvi")
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)
}
//fmt.Printf("%s\n", bodyText)
var dictResponse DictResponse
err=json.Unmarshal(bodyText,&dictResponse)
if err!=nil{
	log.Fatal(err)
}
fmt.Printf("$$$$this is %v#\n",dictResponse)
fmt.Println(dictResponse.Dictionary.Prons,"uk")
for _,item :=range dictResponse.Dictionary.Explanations{
	fmt.Println((item))
}

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

2.第一个例子相对简单,但是由于不熟悉vscode所以运行有问题,好在补了一些基础

package main

import ( "bufio" "fmt" "os" "strconv"

//"os/exec"
"math/rand"
"strings"
"time"

)

func main() { maxn := 100 rand.Seed(time.Now().Unix()) secretNum := rand.Intn(maxn) fmt.Println("this is secret num", secretNum) fmt.Println(os.Args) fmt.Println("Please input ur num") reader := bufio.NewReader(os.Stdin) input, err := reader.ReadString('\n') if err != nil { fmt.Println("this is an error,pls try again", err) //return } input = strings.TrimSuffix(input, "\n") guess, err := strconv.Atoi(input) if err != nil { fmt.Println("invaild input pls try again") //return } fmt.Println("ur guess is", guess) if guess > secretNum { fmt.Println("it is a large num") } else if guess == secretNum { fmt.Println("correct") //break } else { fmt.Println("its a small num") } }

//run ctrl+f5