赞
踩
package main import ( "bufio" "fmt" "math/rand" "os" "strconv" "strings" "time" ) func main() { maxNum := 100 rand.Seed(time.Now().UnixNano()) //生成随机数种子 secretNumber := rand.Intn(maxNum) //生成一个0-100随机数 fmt.Println("要猜测的数字是 ", secretNumber) fmt.Println("请输入你猜测数的数字") reader := bufio.NewReader(os.Stdin) //从键盘读入 for { input, err := reader.ReadString('\n') if err != nil { fmt.Println("输入出错") continue } input = strings.TrimSuffix(input, "\n")//把结尾的换行符去掉 guess, err := strconv.Atoi(input)//字符串转成int整数 if err != nil { fmt.Println("无效输入,请输入一个整数") continue } fmt.Println("你的猜测是 ", guess) if guess > secretNumber { fmt.Println("你的猜测大于secretNumber,请重新输入") } else if guess < secretNumber { fmt.Println("你的猜测小于secretNumber,请重新输入") } else { fmt.Println("猜对了") break } } }
右键检查,找到请求,复制cUrl,
用刚刚复制的cUrl生成代码,链接
代码
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", "zh-CN,zh;q=0.9") 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) }
运行,就输出了一串json格式的字符串。
package main import ( "bufio" "fmt" "math/rand" "os" "strconv" "strings" "time" ) func main() { maxNum := 100 rand.Seed(time.Now().UnixNano()) secretNumber := rand.Intn(maxNum) // fmt.Println("The secret number is ", secretNumber) fmt.Println("Please input your guess") reader := bufio.NewReader(os.Stdin) for { input, err := reader.ReadString('\n') if err != nil { fmt.Println("An error occured while reading input. Please try again", err) continue } input = strings.TrimSuffix(input, "\n") guess, err := strconv.Atoi(input) if err != nil { fmt.Println("Invalid input. Please enter an integer value") continue } fmt.Println("You guess is", guess) if guess > secretNumber { fmt.Println("Your guess is bigger than the secret number. Please try again") } else if guess < secretNumber { fmt.Println("Your guess is smaller than the secret number. Please try again") } else { fmt.Println("Correct, you Legend!") break } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。