第一节课Go基础 | 青训营

150 阅读4分钟

前言

本文记录了跟随青训营学习的一些课堂内容知识以及课堂相关内容的代码实现,同时还包括了一些本人认为应该去了解和拓展的相关内容。

(距离结营结束还剩一周多开始赶笔记0.0)

如有问题,请大家多多指点!

课堂内容知识总结

介绍和发展

首先,什么是Go语言?

Go是一门编译型语言Go语言的工具链将源代码及其依赖转换成计算机的机器指令(译注:静态编译)。

——《Go语言起源》

他的特点:

  • 高性能、高并发
  • 语法简单、学习曲线平缓
  • 丰富的标准库
  • 完善的工具链
  • 静态链接
  • 快速编译
  • 跨平台
  • 垃圾回收

借用《Go语言起源》中展示下Go语言的发展和设计过程中一些对其有影响的编程语言:

img

开发环境的配置

Mac、Windows、Linux均支持Golang,我们可以从golang.google.cn/dl/中选择相应的版本和对应平台的安装文件。国内的代理网站有七牛云的goproxy.cn/

image.png

安装后需要安装目录下的bin文件绝对路径加入到path环境变量中。

安装完毕后,在命令行中输入go version会显示go的版本号:

image.png

在命令行中输入go env会显示一些相关配置的设置信息:

image.png

开发工具本人用的是Goland,可在www.jetbrains.com/go/中下载安装包,在校大学生可用教育政策通过学校的邮箱注册账号免费试用,试用期为一年,试用期过了之后可重新续订使用。 (也可以下载插件跳过goland的检查)

在goland中新建项目,选择自己的Go语言版本以及设置项目名字即可插件项目,新建后goland会自动帮你生成项目的go.mod,里面是项目中引用的一些第三方库或者模块。也可以在终端中输入go mod init进行项目环境的初始化。

然后新建main.go文件即可开始coding。

基础语法

跳过。。。。

课堂实践例子

1、猜谜游戏

该例子中涉及到了输入输出、随机数、逻辑判断的基础语法内容。课程中老师指出rand.Intn()获取随机数需要设置随机数种子来获取随机数:

rand.Seed(time.Now().UnixNano())
# 通过时间戳初始化

否则获取的随机数每次都是一致的。

但是本人和同学没设置随机数种子之前随机数每次也不相同,然后发现输入rand.Seed()已经弃用:

Seed uses the provided seed value to initialize the default Source to a deterministic state. Seed values that have the same remainder when divided by 2³¹-1 generate the same pseudo-random sequence. Seed, unlike the Rand.Seed method, is safe for concurrent use. If Seed is not called, the generator is seeded randomly at program startup. Prior to Go 1.20, the generator was seeded like Seed(1) at program startup. To force the old behavior, call Seed(1) at program startup. Alternately, set GODEBUG=randautoseed=0 in the environment before making any calls to functions in this package. Deprecated: Programs that call Seed and then expect a specific sequence of results from the global random source (using functions such as Int) can be broken when a dependency changes how much it consumes from the global random source. To avoid such breakages, programs that need a specific result sequence should use NewRand(NewSource(seed)) to obtain a random generator that other packages cannot access.

1.20之后的版本不需要设置随机数种子,会在启动时随机设置。(我的版本是1.20.1)

2、在线字典

课程中,例子是模拟彩云小译的一次翻译过程,在golang中模仿发送翻译过程中向接口POST的请求,我们可以通过浏览器的开发者模式复制该请求:

image.png

然后我们在curlconverter.com/go/粘贴刚刚复制的请求,然后会生成对应的请求代码:

image.png

但是,其中的代码是固定的,输入的数据data也是固定的,我们需要将其中的request body抽离出来,这样我们就可以将想要翻译的单词当做变量,模拟整个翻译。

首先,将浏览器中返回的接口结构解析成Golang中的结构体字段,可以借助oktools.net/json2go代码生成工具进行解析,直接自动生成相应的结构体:

image.png

然后我们在代码中定义发送和接收的结构体DictRequestDictResponse

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      []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"`
}

随后将想要查的单词和转换方式保存在DictRequest中,用DictResponse接收接口返回的数据,最后解析出相要的内容。