CSDIY:这是一个非科班学生的努力之路,从今天开始这个系列会长期更新,(最好做到日更),我会慢慢把自己目前对CS的努力逐一上传,帮助那些和我一样有着梦想的玩家取得胜利!!!
第一弹:Cpp零基础学习【30 DAYS 从0到1】
第二弹:Go开发入门【字节后端青训营】
ByteDance青训营 后端方向 00 Go 语言入门
Go 语言快速上手
1.2 什么是 Go 语言
- 高性能、高并发
- 语法简单、学习曲线平缓(入门到开发项目只需要一周时间)
- 丰富的标准库(类似 Cpp 有很多标准库,减少第三方库的使用)
- 完善的的工具链(代码补充、代码管理等)
- 静态链接(只需要一个可执行文件即可,部署方便)
- 快速编译
- 跨平台(跨Window/Linux/Unix/Mac)
- 垃圾回收(自动内存分配和释放)
1.2 哪些公司在用
- ByteDance
- Tencent
- DiDi
- Bilibili
- Meituan
1.3 为什么 ByteDance 使用 Go
- C++不适合在线Web
- 性能优秀、部署简单、学习成本低
- 内部 RPC 和 HTTP 框架的推广
2.1 开发环境
- Golang
2.2 基础语法
Hello World
package mainimport "fmt"func main() {fmt.Println("Hello, World!")
}
变量
package mainimport ("fmt""math"
)func main() {var a = "initial"var b, c int = 1, 2var d = truevar e float64f := float32(e)g := a + "foo"fmt.Println(a, b, c, d, e, f) // initial 1 2 true 0 0fmt.Println(g) // initialappleconst s string = "constant"const h = 500000000const i = 3e20 / hfmt.Println(s, h, i, math.Sin(h), math.Sin(i))
}
if else
package mainimport "fmt"func main() {if 7%2 == 0 {fmt.Println("7 is even")} else {fmt.Println("7 is odd")}if 8%4 == 0 {fmt.Println("8 is divisible by 4")}if num := 9; num < 0 {fmt.Println(num, "is negative")} else if num < 10 {fmt.Println(num, "has 1 digit")} else {fmt.Println(num, "has multiple digits")}
}
循环
package mainimport "fmt"func main() {i := 1for {fmt.Println("loop")break}for j := 7; j < 9; j++ {fmt.Println(j)}for n := 0; n < 5; n++ {if n%2 == 0 {continue}fmt.Println(n)}for i <= 3 {fmt.Println(i)i = i + 1}
}
switch
自动执行break
package mainimport ("fmt""time"
)func main() {a := 2switch a {case 1:fmt.Println("one")case 2:fmt.Println("two")case 3:fmt.Println("three")case 4, 5:fmt.Println("four or five")default:fmt.Println("other")}t := time.Now()switch {case t.Hour() < 12:fmt.Println("It's before noon")default:fmt.Println("It's after noon")}
}
数组
package mainimport "fmt"func main() {var a [5]inta[4] = 100fmt.Println("get:", a[2])fmt.Println("len:", len(a))b := [5]int{1, 2, 3, 4, 5}fmt.Println(b)var twoD [2][3]intfor i := 0; i < 2; i++ {for j := 0; j < 3; j++ {twoD[i][j] = i + j}}fmt.Println("2d: ", twoD)
}
切片
package mainimport "fmt"func main() {s := make([]string, 3) // 创建切片 Ss[0] = "a"s[1] = "b"s[2] = "c"fmt.Println("get:", s[2]) // cfmt.Println("len:", len(s)) // 3s = append(s, "d")s = append(s, "e", "f")fmt.Println(s) // [a b c d e f]c := make([]string, len(s))copy(c, s)fmt.Println(c) // [a b c d e f]fmt.Println(s[2:5]) // [c d e] 从 2 到 5(不含)fmt.Println(s[:5]) // [a b c d e]fmt.Println(s[2:]) // [c d e f]good := []string{"g", "o", "o", "d"}fmt.Println(good) // [g o o d]
}
map
package mainimport "fmt"func main() {m := make(map[string]int)m["one"] = 1m["two"] = 2fmt.Println(m) // map[one:1 two:2]fmt.Println(len(m)) // 2fmt.Println(m["one"]) // 1fmt.Println(m["unknow"]) // 0r, ok := m["unknow"]fmt.Println(r, ok) // 0 falsedelete(m, "one")m2 := map[string]int{"one": 1, "two": 2}var m3 = map[string]int{"one": 1, "two": 2}fmt.Println(m2, m3)
}
range
package mainimport "fmt"func main() {nums := []int{2, 3, 4}sum := 0for i, num := range nums {sum += numif num == 2 {fmt.Println("index:", i, "num:", num) // index: 0 num: 2}}fmt.Println(sum) // 9m := map[string]string{"a": "A", "b": "B"}for k, v := range m {fmt.Println(k, v) // b 8; a A}for k := range m {fmt.Println("key", k) // key a; key b}
}
函数
函数会有两个结果(一个是函数返回值,一个是错误结果)
package mainimport "fmt"func add(a int, b int) int {return a + b
}func add2(a, b int) int {return a + b
}func exists(m map[string]string, k string) (v string, ok bool) {v, ok = m[k]return v, ok // 两个返回值 ok 表示错误结果
}func main() {res := add(1, 2)fmt.Println(res) // 3v, ok := exists(map[string]string{"a": "A"}, "a")fmt.Println(v, ok) // A True
}
指针
package mainimport "fmt"// 实际上无效, 因为传入的是copy的参数
func add2(n int) {n += 2
}func add2ptr(n *int) {*n += 2
}func main() {n := 5add2(n)fmt.Println(n) // 5add2ptr(&n)fmt.Println(n) // 7
}
结构体
package mainimport "fmt"type user struct {name stringpassword string
}func main() {// 初始化结构体变量a := user{name: "wang", password: "1024"}b := user{"wang", "1024"}c := user{name: "wang"}c.password = "1024"var d user// 像 C 语言一样初始化、赋值操作d.name = "wang"d.password = "1024"fmt.Println(a, b, c, d) // {wang 1024} {wang 1024} {wang 1024} {wang 1024}fmt.Println(checkPassword(a, "haha")) // falsefmt.Println(checkPassword2(&a, "haha")) // false
}func checkPassword(u user, password string) bool {return u.password == password
}func checkPassword2(u *user, password string) bool {return u.password == password
}
结构体方法
package mainimport "fmt"type user struct {name stringpassword string
}// 结构体函数
func (u user) checkPassword(password string) bool {return u.password == password
}func (u *user) resetPassword(password string) {u.password = password
}func main() {a := user{name: "wang", password: "1024"}a.resetPassword("2048")fmt.Println(a.checkPassword("2048")) // true
}
错误处理
对于可能发生错误的函数,可以返回一个错误信息,结合 if else 提高性能
package mainimport ("errors""fmt"
)type user struct {name stringpassword string
}func findUser(users []user, name string) (v *user, err error) {for _, u := range users {if u.name == name {return &u, nil}}return nil, errors.New("not found")
}func main() {u, err := findUser([]user{{"wang", "1024"}}, "wang")if err != nil {fmt.Println(err)return}fmt.Println(u.name) // wangif u, err := findUser([]user{{"wang", "1024"}}, "li"); err != nil {fmt.Println(err) // not foundreturn} else {fmt.Println(u.name)}
}
字符串处理
go 语言字符串可以直接相加结合
package mainimport ("fmt""strings"
)func main() {a := "hello"fmt.Println(strings.Contains(a, "ll")) // truefmt.Println(strings.Count(a, "l")) // 2fmt.Println(strings.HasPrefix(a, "he")) // truefmt.Println(strings.HasSuffix(a, "llo")) // truefmt.Println(strings.Index(a, "ll")) // 2fmt.Println(strings.Join([]string{"he", "llo"}, "-")) // he-llofmt.Println(strings.Repeat(a, 2)) // hellohellofmt.Println(strings.Replace(a, "e", "E", -1)) // hEllofmt.Println(strings.Split("a-b-c", "-")) // [a b c]fmt.Println(strings.ToLower(a)) // hellofmt.Println(strings.ToUpper(a)) // HELLOfmt.Println(len(a)) // 5b := "你好"fmt.Println(len(b)) // 6
}
字符串格式化
可以用 %v 通用表示:字符串,数字,结构体值
%+v打印:结构体字段名字、值
%#v打印:结构体名称、字段名字、值
也可以用 %.2f 打印小数点
package mainimport "fmt"type point struct {x, y int
}func main() {s := "hello"n := 123p := point{1, 2}fmt.Println(s, n) // hello 123fmt.Println(p) // {1 2}fmt.Printf("s=%v\n", s) // s=hellofmt.Printf("n=%v\n", n) // n=123fmt.Printf("p=%v\n", p) // p={1 2}fmt.Printf("p=%+v\n", p) // p={x:1 y:2}fmt.Printf("p=%#v\n", p) // p=main.point{x:1, y:2}f := 3.141592653fmt.Println(f) // 3.141592653fmt.Printf("%.2f\n", f) // 3.14
}
JSON 处理
package mainimport ("encoding/json""fmt"
)type userInfo struct {Name stringAge int `json:"age"`Hobby []string
}func main() {a := userInfo{Name: "wang", Age: 18, Hobby: []string{"Golang", "TypeScript"}}buf, err := json.Marshal(a)if err != nil {panic(err)}fmt.Println(buf) // [123 34 78 97...]fmt.Println(string(buf)) // {"Name":"wang","age":18,"Hobby":["Golang","TypeScript"]}buf, err = json.MarshalIndent(a, "", "\t")if err != nil {panic(err)}fmt.Println(string(buf))var b userInfoerr = json.Unmarshal(buf, &b)if err != nil {panic(err)}fmt.Printf("%#v\n", b) // main.userInfo{Name:"wang", Age:18, Hobby:[]string{"Golang", "TypeScript"}}
}
时间处理
package mainimport ("fmt""time"
)func main() {now := time.Now()fmt.Println(now) // 2022-03-27 18:04:59.433297 +0800 CST m=+0.000087933t := time.Date(2022, 3, 27, 1, 25, 36, 0, time.UTC)t2 := time.Date(2022, 3, 27, 2, 30, 36, 0, time.UTC)fmt.Println(t) // 2022-03-27 01:25:36 +0000 UTCfmt.Println(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute()) // 2022 March 27 1 25fmt.Println(t.Format("2006-01-02 15:04:05")) // 2022-03-27 01:25:36// 可以计算两个时间之间的时间段diff := t2.Sub(t)fmt.Println(diff) // 1h5m0sfmt.Println(diff.Minutes(), diff.Seconds()) // 65 3900// 可以将字符串解析为时间戳t3, err := time.Parse("2006-01-02 15:04:05", "2022-03-27 01:25:36")if err != nil {panic(err)}fmt.Println(t3 == t) // truefmt.Println(now.Unix()) // 1648738080
}
数字解析
数字和字符串互相转化
package mainimport ("fmt""strconv"
)func main() {f, _ := strconv.ParseFloat("1.234", 64)fmt.Println(f) // 1.234n, _ := strconv.ParseInt("111", 10, 64)fmt.Println(n) // 111n, _ = strconv.ParseInt("0x1000", 0, 64)fmt.Println(n) // 4096n2, _ := strconv.Atoi("123")fmt.Println(n2) // 123n2, err := strconv.Atoi("AAA")fmt.Println(n2, err) // 0 strconv.Atoi: parsing "AAA": invalid syntax
}
进程信息
package mainimport ("fmt""os""os/exec"
)func main() {// go run example/20-env/main.go a b c dfmt.Println(os.Args) // [/var/folders/8p/n34xxfnx38dg8bv_x8l62t_m0000gn/T/go-build3406981276/b001/exe/main a b c d]// 环境变量fmt.Println(os.Getenv("PATH")) // /usr/local/go/bin...fmt.Println(os.Setenv("AA", "BB"))// 输入输出buf, err := exec.Command("grep", "127.0.0.1", "/etc/hosts").CombinedOutput()if err != nil {panic(err)}fmt.Println(string(buf)) // 127.0.0.1 localhost
}
3.1 猜谜游戏
3.1.1 生成随机数
package mainimport ("fmt""math/rand"
)func main() {maxNum := 100// 使用 rand 包来生成随机数secretNumber := rand.Intn(maxNum)fmt.Println("The secret number is ", secretNumber)
}
输出发现每次产生的随机数都会生成相同的数字,需要加上随机数种子
3.1.2 生成随机数 V2
package mainimport ("fmt""math/rand""time"
)func main() {maxNum := 100// 添加时间随机数种子,达到随机目的rand.Seed(time.Now().UnixNano())secretNumber := rand.Intn(maxNum)fmt.Println("The secret number is ", secretNumber)
}
3.1.3 读取用户输入
package mainimport ("bufio" // 读取输入输出的包"fmt""math/rand""os""strconv""strings""time"
)func main() {maxNum := 100rand.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)input, err := reader.ReadString('\n')if err != nil {fmt.Println("An error occured while reading input. Please try again", err)return}// 去掉换行符input = strings.Trim(input, "\r\n")// 字符串转换为数字:Atoiguess, err := strconv.Atoi(input)// 转换失败的时候 输出错误消息if err != nil {fmt.Println("Invalid input. Please enter an integer value")return}fmt.Println("You guess is", guess)
}
3.1.4 实现判断逻辑
package mainimport ("bufio""fmt""math/rand""os""strconv""strings""time"
)func main() {maxNum := 100rand.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)input, err := reader.ReadString('\n')if err != nil {fmt.Println("An error occured while reading input. Please try again", err)return}input = strings.Trim(input, "\r\n")guess, err := strconv.Atoi(input)if err != nil {fmt.Println("Invalid input. Please enter an integer value")return}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!")}
}
添加了是否判断正确的逻辑,但是用户只能输入一次,此时需要考虑添加循环,实现用户多次输入。
3.1.5 实现游戏循环
package mainimport ("bufio""fmt""math/rand""os""strconv""strings""time"
)func main() {maxNum := 100rand.Seed(time.Now().UnixNano())secretNumber := rand.Intn(maxNum)// 屏蔽随机数结果// fmt.Println("The secret number is ", secretNumber)fmt.Println("Please input your guess")fmt.scanf("&num")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.Trim(input, "\r\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 跳出循环break}}
}
3.2 在线词典
调用第三方 API 进行在线查询
3.2.1 抓包
调用 API : https://fanyi.caiyunapp.com/#/
curl 'https://api.interpreter.caiyunai.com/v1/dict' \-H 'accept: application/json, text/plain, */*' \-H 'accept-language: zh' \-H 'app-name: xiaoyi' \-H 'authorization: Bearer' \-H 'content-type: application/json;charset=UTF-8' \-H 'device-id: d7a3b39cabbf9091dd99920e3812ff88' \-H 'origin: https://fanyi.caiyunapp.com' \-H 'os-type: web' \-H 'os-version;' \-H 'priority: u=1, i' \-H 'referer: https://fanyi.caiyunapp.com/' \-H 'sec-ch-ua: "Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"' \-H 'sec-ch-ua-mobile: ?0' \-H 'sec-ch-ua-platform: "Windows"' \-H 'sec-fetch-dest: empty' \-H 'sec-fetch-mode: cors' \-H 'sec-fetch-site: cross-site' \-H 'sec-gpc: 1' \-H 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36' \-H 'x-authorization: token:qgemv4jr1y38jyq6vhvi' \--data-raw '{"trans_type":"en2zh","source":"hello"}'
3.2.2 代码生成
将上面的代码转化为 go 语言代码
代码生成:https://curlconverter.com/go/
package mainimport ("fmt""io""log""net/http""strings"
)func main() {client := &http.Client{}var data = strings.NewReader(`{"trans_type":"en2zh","source":"hello"}`)// 创建请求:POST请求头,data流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")req.Header.Set("app-name", "xiaoyi")req.Header.Set("authorization", "Bearer")req.Header.Set("content-type", "application/json;charset=UTF-8")req.Header.Set("device-id", "d7a3b39cabbf9091dd99920e3812ff88")req.Header.Set("origin", "https://fanyi.caiyunapp.com")req.Header.Set("os-type", "web")req.Header.Set("os-version", "")req.Header.Set("priority", "u=1, i")req.Header.Set("referer", "https://fanyi.caiyunapp.com/")req.Header.Set("sec-ch-ua", `"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"`)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("sec-gpc", "1")req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36")req.Header.Set("x-authorization", "token:qgemv4jr1y38jyq6vhvi")// 发起请求:返回resp和errresp, err := client.Do(req)// 如果产生err会直接退出进程if err != nil {log.Fatal(err)}// 收到相应后立马关闭defer resp.Body.Close()// 读取响应bodyText, err := io.ReadAll(resp.Body)// 响应err也会退出进程if err != nil {log.Fatal(err)}fmt.Printf("%s\n", bodyText)
}
3.2.3 生成 request body
添加 JSON 字段 和 3.2.2 输出结果应当相同
package mainimport ("bytes""encoding/json""fmt""io""log""net/http"
)type DictRequest struct {TransType string `json:"trans_type"`Source string `json:"source"`UserID string `json:"user_id"`
}func main() {client := &http.Client{}// 初始化 request 结构变量request := DictRequest{TransType: "en2zh", Source: "hello"}buf, err := json.Marshal(request)if err != nil {log.Fatal(err)}// 转化为 datavar data = bytes.NewReader(buf)// 创建请求:POST请求头,data流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")req.Header.Set("app-name", "xiaoyi")req.Header.Set("authorization", "Bearer")req.Header.Set("content-type", "application/json;charset=UTF-8")req.Header.Set("device-id", "d7a3b39cabbf9091dd99920e3812ff88")req.Header.Set("origin", "https://fanyi.caiyunapp.com")req.Header.Set("os-type", "web")req.Header.Set("os-version", "")req.Header.Set("priority", "u=1, i")req.Header.Set("referer", "https://fanyi.caiyunapp.com/")req.Header.Set("sec-ch-ua", `"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"`)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("sec-gpc", "1")req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36")req.Header.Set("x-authorization", "token:qgemv4jr1y38jyq6vhvi")// 发起请求:返回resp和errresp, err := client.Do(req)// 如果产生err会直接退出进程if err != nil {log.Fatal(err)}// 收到相应后立马关闭defer resp.Body.Close()// 读取响应bodyText, err := io.ReadAll(resp.Body)// 响应err也会退出进程if err != nil {log.Fatal(err)}fmt.Printf("%s\n", bodyText)
}
3.2.4 解析 response body
代码生成:https://oktools.iokde.com/json2go
package mainimport ("bytes""encoding/json""fmt""io""log""net/http"
)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 main() {client := &http.Client{}// 初始化 request 结构变量request := DictRequest{TransType: "en2zh", Source: "hello"}buf, err := json.Marshal(request)if err != nil {log.Fatal(err)}// 转化为 datavar data = bytes.NewReader(buf)// 创建请求:POST请求头,data流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")req.Header.Set("app-name", "xiaoyi")req.Header.Set("authorization", "Bearer")req.Header.Set("content-type", "application/json;charset=UTF-8")req.Header.Set("device-id", "d7a3b39cabbf9091dd99920e3812ff88")req.Header.Set("origin", "https://fanyi.caiyunapp.com")req.Header.Set("os-type", "web")req.Header.Set("os-version", "")req.Header.Set("priority", "u=1, i")req.Header.Set("referer", "https://fanyi.caiyunapp.com/")req.Header.Set("sec-ch-ua", `"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"`)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("sec-gpc", "1")req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36")req.Header.Set("x-authorization", "token:qgemv4jr1y38jyq6vhvi")// 发起请求:返回resp和errresp, err := client.Do(req)// 如果产生err会直接退出进程if err != nil {log.Fatal(err)}// 收到相应后立马关闭defer resp.Body.Close()// 读取响应bodyText, err := io.ReadAll(resp.Body)// 响应err也会退出进程if err != nil {log.Fatal(err)}// fmt.Printf("%s\n", bodyText)var dictResponse DictResponseerr = json.Unmarshal(bodyText, &dictResponse)if err != nil {log.Fatal(err)}fmt.Printf("%#v\n", dictResponse)
}
3.2.5 打印结果/完善代码
// 简易单词在线查询
// 请在新建终端中进行操作
// 输入参数:1:go run "d:\VSCode\Go\Code\Helloworld.go" 2:单词 空格间隔
// 输入样例:go run "d:\VSCode\Go\Code\Helloworld.go" hello
// 输出样例:hello UK: [ˈheˈləu] US: [həˈlo]
// int.喂;哈罗
// n.引人注意的呼声
// v.向人呼(喂)package mainimport ("bytes""encoding/json""fmt""io""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 结构变量request := DictRequest{TransType: "en2zh", Source: word}buf, err := json.Marshal(request)if err != nil {log.Fatal(err)}// 转化为 datavar data = bytes.NewReader(buf)// 创建请求:POST请求头,data流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")req.Header.Set("app-name", "xiaoyi")req.Header.Set("authorization", "Bearer")req.Header.Set("content-type", "application/json;charset=UTF-8")req.Header.Set("device-id", "d7a3b39cabbf9091dd99920e3812ff88")req.Header.Set("origin", "https://fanyi.caiyunapp.com")req.Header.Set("os-type", "web")req.Header.Set("os-version", "")req.Header.Set("priority", "u=1, i")req.Header.Set("referer", "https://fanyi.caiyunapp.com/")req.Header.Set("sec-ch-ua", `"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"`)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("sec-gpc", "1")req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36")req.Header.Set("x-authorization", "token:qgemv4jr1y38jyq6vhvi")// 发起请求:返回resp和errresp, err := client.Do(req)// 如果产生err会直接退出进程if err != nil {log.Fatal(err)}// 收到相应后立马关闭defer resp.Body.Close()// 读取响应bodyText, err := io.ReadAll(resp.Body)// 响应err也会退出进程if err != nil {log.Fatal(err)}// 防御性编程,有可能是404等出错码if resp.StatusCode != 200 {log.Fatal("bad StatusCode:", resp.StatusCode, string(bodyText))}// fmt.Printf("%s\n", bodyText)var dictResponse DictResponseerr = json.Unmarshal(bodyText, &dictResponse)if err != nil {log.Fatal(err)}// fmt.Printf("%#v\n", dictResponse)fmt.Println(word, "UK:", dictResponse.Dictionary.Prons.En, "US:", dictResponse.Dictionary.Prons.EnUs)for _, item := range dictResponse.Dictionary.Explanations {fmt.Println(item)}
}// 主函数
func main() {// 输入参数:1:go run "d:\VSCode\Go\Code\Helloworld.go" 2:单词 空格间隔// 输入样例:go run "d:\VSCode\Go\Code\Helloworld.go" hello// 若样例不足两个,返回错误,退出进程if len(os.Args) != 2 {fmt.Fprintf(os.Stderr, `usage: simpleDict WORD
example: simpleDict hello`)os.Exit(1)}word := os.Args[1]query(word)
}
3.3 SOCKS5 代理服务器
某些企业为了网络安全会拥有自己的代理服务器
原理:
3.3.1 TCP echo server
package mainimport ("bufio""log""net"
)func main() {server, err := net.Listen("tcp", "127.0.0.1:1080")if err != nil {panic(err)}for {client, err := server.Accept()if err != nil {log.Printf("Accept failed %v", err)continue}// 启动一个子线程进行开销go process(client)}
}func process(conn net.Conn) {// 关闭链接,生命周期相等defer conn.Close()// 创建带缓冲流reader := bufio.NewReader(conn)// for死循环for {b, err := reader.ReadByte()if err != nil {break}// 写入一个字节_, err = conn.Write([]byte{b})if err != nil {break}}
}
3.3.2 auth
package mainimport ("bufio""fmt""io""log""net"
)const socks5Ver = 0x05
const cmdBind = 0x01
const atypeIPV4 = 0x01
const atypeHOST = 0x03
const atypeIPV6 = 0x04func main() {server, err := net.Listen("tcp", "127.0.0.1:1080")if err != nil {panic(err)}for {client, err := server.Accept()if err != nil {log.Printf("Accept failed %v", err)continue}go process(client)}
}func process(conn net.Conn) {defer conn.Close()reader := bufio.NewReader(conn)err := auth(reader, conn)if err != nil {log.Printf("client %v auth failed:%v", conn.RemoteAddr(), err)return}log.Println("auth success")
}func auth(reader *bufio.Reader, conn net.Conn) (err error) {// +----+----------+----------+// |VER | NMETHODS | METHODS |// +----+----------+----------+// | 1 | 1 | 1 to 255 |// +----+----------+----------+// VER: 协议版本,socks5为0x05// NMETHODS: 支持认证的方法数量// METHODS: 对应NMETHODS,NMETHODS的值为多少,METHODS就有多少个字节。RFC预定义了一些值的含义,内容如下:// X’00’ NO AUTHENTICATION REQUIRED// X’02’ USERNAME/PASSWORDver, err := reader.ReadByte()if err != nil {return fmt.Errorf("read ver failed:%w", err)}if ver != socks5Ver {return fmt.Errorf("not supported ver:%v", ver)}methodSize, err := reader.ReadByte()if err != nil {return fmt.Errorf("read methodSize failed:%w", err)}method := make([]byte, methodSize)_, err = io.ReadFull(reader, method)if err != nil {return fmt.Errorf("read method failed:%w", err)}log.Println("ver", ver, "method", method)// +----+--------+// |VER | METHOD |// +----+--------+// | 1 | 1 |// +----+--------+_, err = conn.Write([]byte{socks5Ver, 0x00})if err != nil {return fmt.Errorf("write failed:%w", err)}return nil
}
3.3.3 请求阶段
package mainimport ("bufio""encoding/binary""errors""fmt""io""log""net"
)const socks5Ver = 0x05
const cmdBind = 0x01
const atypeIPV4 = 0x01
const atypeHOST = 0x03
const atypeIPV6 = 0x04func main() {server, err := net.Listen("tcp", "127.0.0.1:1080")if err != nil {panic(err)}for {client, err := server.Accept()if err != nil {log.Printf("Accept failed %v", err)continue}go process(client)}
}func process(conn net.Conn) {defer conn.Close()reader := bufio.NewReader(conn)err := auth(reader, conn)if err != nil {log.Printf("client %v auth failed:%v", conn.RemoteAddr(), err)return}err = connect(reader, conn)if err != nil {log.Printf("client %v auth failed:%v", conn.RemoteAddr(), err)return}
}func auth(reader *bufio.Reader, conn net.Conn) (err error) {// +----+----------+----------+// |VER | NMETHODS | METHODS |// +----+----------+----------+// | 1 | 1 | 1 to 255 |// +----+----------+----------+// VER: 协议版本,socks5为0x05// NMETHODS: 支持认证的方法数量// METHODS: 对应NMETHODS,NMETHODS的值为多少,METHODS就有多少个字节。RFC预定义了一些值的含义,内容如下:// X’00’ NO AUTHENTICATION REQUIRED// X’02’ USERNAME/PASSWORDver, err := reader.ReadByte()if err != nil {return fmt.Errorf("read ver failed:%w", err)}if ver != socks5Ver {return fmt.Errorf("not supported ver:%v", ver)}methodSize, err := reader.ReadByte()if err != nil {return fmt.Errorf("read methodSize failed:%w", err)}method := make([]byte, methodSize)_, err = io.ReadFull(reader, method)if err != nil {return fmt.Errorf("read method failed:%w", err)}// +----+--------+// |VER | METHOD |// +----+--------+// | 1 | 1 |// +----+--------+_, err = conn.Write([]byte{socks5Ver, 0x00})if err != nil {return fmt.Errorf("write failed:%w", err)}return nil
}func connect(reader *bufio.Reader, conn net.Conn) (err error) {// +----+-----+-------+------+----------+----------+// |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |// +----+-----+-------+------+----------+----------+// | 1 | 1 | X'00' | 1 | Variable | 2 |// +----+-----+-------+------+----------+----------+// VER 版本号,socks5的值为0x05// CMD 0x01表示CONNECT请求// RSV 保留字段,值为0x00// ATYP 目标地址类型,DST.ADDR的数据对应这个字段的类型。// 0x01表示IPv4地址,DST.ADDR为4个字节// 0x03表示域名,DST.ADDR是一个可变长度的域名// DST.ADDR 一个可变长度的值// DST.PORT 目标端口,固定2个字节buf := make([]byte, 4)_, err = io.ReadFull(reader, buf)if err != nil {return fmt.Errorf("read header failed:%w", err)}ver, cmd, atyp := buf[0], buf[1], buf[3]if ver != socks5Ver {return fmt.Errorf("not supported ver:%v", ver)}if cmd != cmdBind {return fmt.Errorf("not supported cmd:%v", cmd)}addr := ""switch atyp {case atypeIPV4:_, err = io.ReadFull(reader, buf)if err != nil {return fmt.Errorf("read atyp failed:%w", err)}addr = fmt.Sprintf("%d.%d.%d.%d", buf[0], buf[1], buf[2], buf[3])case atypeHOST:hostSize, err := reader.ReadByte()if err != nil {return fmt.Errorf("read hostSize failed:%w", err)}host := make([]byte, hostSize)_, err = io.ReadFull(reader, host)if err != nil {return fmt.Errorf("read host failed:%w", err)}addr = string(host)case atypeIPV6:return errors.New("IPv6: no supported yet")default:return errors.New("invalid atyp")}_, err = io.ReadFull(reader, buf[:2])if err != nil {return fmt.Errorf("read port failed:%w", err)}port := binary.BigEndian.Uint16(buf[:2])log.Println("dial", addr, port)// +----+-----+-------+------+----------+----------+// |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |// +----+-----+-------+------+----------+----------+// | 1 | 1 | X'00' | 1 | Variable | 2 |// +----+-----+-------+------+----------+----------+// VER socks版本,这里为0x05// REP Relay field,内容取值如下 X’00’ succeeded// RSV 保留字段// ATYPE 地址类型// BND.ADDR 服务绑定的地址// BND.PORT 服务绑定的端口DST.PORT_, err = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0})if err != nil {return fmt.Errorf("write failed: %w", err)}return nil
}
3.3.4 relay 阶段
package mainimport ("bufio""context""encoding/binary""errors""fmt""io""log""net"
)const socks5Ver = 0x05
const cmdBind = 0x01
const atypeIPV4 = 0x01
const atypeHOST = 0x03
const atypeIPV6 = 0x04func main() {server, err := net.Listen("tcp", "127.0.0.1:1080")if err != nil {panic(err)}for {client, err := server.Accept()if err != nil {log.Printf("Accept failed %v", err)continue}go process(client)}
}func process(conn net.Conn) {defer conn.Close()reader := bufio.NewReader(conn)err := auth(reader, conn)if err != nil {log.Printf("client %v auth failed:%v", conn.RemoteAddr(), err)return}err = connect(reader, conn)if err != nil {log.Printf("client %v auth failed:%v", conn.RemoteAddr(), err)return}
}func auth(reader *bufio.Reader, conn net.Conn) (err error) {// +----+----------+----------+// |VER | NMETHODS | METHODS |// +----+----------+----------+// | 1 | 1 | 1 to 255 |// +----+----------+----------+// VER: 协议版本,socks5为0x05// NMETHODS: 支持认证的方法数量// METHODS: 对应NMETHODS,NMETHODS的值为多少,METHODS就有多少个字节。RFC预定义了一些值的含义,内容如下:// X’00’ NO AUTHENTICATION REQUIRED// X’02’ USERNAME/PASSWORDver, err := reader.ReadByte()if err != nil {return fmt.Errorf("read ver failed:%w", err)}if ver != socks5Ver {return fmt.Errorf("not supported ver:%v", ver)}methodSize, err := reader.ReadByte()if err != nil {return fmt.Errorf("read methodSize failed:%w", err)}method := make([]byte, methodSize)_, err = io.ReadFull(reader, method)if err != nil {return fmt.Errorf("read method failed:%w", err)}// +----+--------+// |VER | METHOD |// +----+--------+// | 1 | 1 |// +----+--------+_, err = conn.Write([]byte{socks5Ver, 0x00})if err != nil {return fmt.Errorf("write failed:%w", err)}return nil
}func connect(reader *bufio.Reader, conn net.Conn) (err error) {// +----+-----+-------+------+----------+----------+// |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |// +----+-----+-------+------+----------+----------+// | 1 | 1 | X'00' | 1 | Variable | 2 |// +----+-----+-------+------+----------+----------+// VER 版本号,socks5的值为0x05// CMD 0x01表示CONNECT请求// RSV 保留字段,值为0x00// ATYP 目标地址类型,DST.ADDR的数据对应这个字段的类型。// 0x01表示IPv4地址,DST.ADDR为4个字节// 0x03表示域名,DST.ADDR是一个可变长度的域名// DST.ADDR 一个可变长度的值// DST.PORT 目标端口,固定2个字节buf := make([]byte, 4)_, err = io.ReadFull(reader, buf)if err != nil {return fmt.Errorf("read header failed:%w", err)}ver, cmd, atyp := buf[0], buf[1], buf[3]if ver != socks5Ver {return fmt.Errorf("not supported ver:%v", ver)}if cmd != cmdBind {return fmt.Errorf("not supported cmd:%v", cmd)}addr := ""switch atyp {case atypeIPV4:_, err = io.ReadFull(reader, buf)if err != nil {return fmt.Errorf("read atyp failed:%w", err)}addr = fmt.Sprintf("%d.%d.%d.%d", buf[0], buf[1], buf[2], buf[3])case atypeHOST:hostSize, err := reader.ReadByte()if err != nil {return fmt.Errorf("read hostSize failed:%w", err)}host := make([]byte, hostSize)_, err = io.ReadFull(reader, host)if err != nil {return fmt.Errorf("read host failed:%w", err)}addr = string(host)case atypeIPV6:return errors.New("IPv6: no supported yet")default:return errors.New("invalid atyp")}_, err = io.ReadFull(reader, buf[:2])if err != nil {return fmt.Errorf("read port failed:%w", err)}port := binary.BigEndian.Uint16(buf[:2])dest, err := net.Dial("tcp", fmt.Sprintf("%v:%v", addr, port))if err != nil {return fmt.Errorf("dial dst failed:%w", err)}defer dest.Close()log.Println("dial", addr, port)// +----+-----+-------+------+----------+----------+// |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |// +----+-----+-------+------+----------+----------+// | 1 | 1 | X'00' | 1 | Variable | 2 |// +----+-----+-------+------+----------+----------+// VER socks版本,这里为0x05// REP Relay field,内容取值如下 X’00’ succeeded// RSV 保留字段// ATYPE 地址类型// BND.ADDR 服务绑定的地址// BND.PORT 服务绑定的端口DST.PORT_, err = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0})if err != nil {return fmt.Errorf("write failed: %w", err)}ctx, cancel := context.WithCancel(context.Background())defer cancel()go func() {_, _ = io.Copy(dest, reader)cancel()}()go func() {_, _ = io.Copy(conn, dest)cancel()}()<-ctx.Done()return nil
}