Go学习之旅chapter2

还是跟着Go官方的学习之旅一步步慢慢敲的

对于Go的切片,数组,time  strings ,math等函数有更进一步的了解吧

关于time

如何正确表示时间呢?

公认最准确的计算应该是使用“原子震荡周期”所计算的物理时钟了(Atomic Clock, 也被称为原子钟),这也被定义为标准时间(International Atomic Time)。

而我们常常看见的 UTC(Universal Time Coordinated,世界协调时间)就是利用这种 Atomic Clock 为基准所定义出来的正确时间。UTC 标准时间是以 GMT(Greenwich Mean Time,格林尼治时间)这个时区为主,所以本地时间与 UTC 时间的时差就是本地时间与 GMT 时间的时差。

UTC + 时区差 = 本地时间

国内一般使用的是北京时间,与 UTC 的时间关系如下:

UTC + 8 个小时 = 北京时间

Time函数

   time.Now().xxx

 

 

 

 

 

JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。易于人阅读和编写,可以在多种语言之间进行数据交换 [5]  

 

Defer

//defer 语句会将函数推迟到外层函数返回之后执行。
//推迟调用的函数其参数会立即求值,但直到外层函数返回前该函数都不会被调用。

就是在内的执行后后再轮到defer后面的执行

一如defer的意思,推迟,延迟

结构的为栈 defer栈 后进先出

usual mechanisms  控制机制

It also has the go statement to run code in a separate goroutine

 也有 go 语句在单独的 goroutine 中运行代码。

 

Go语言宕机(panic——程序终止运行

defer statement pushes a function call onto a list.

defer 语句将函数调用推送到列表中

The list of saved calls is executed after the surrounding function returns. 

保存的调用列表在周围函数返回后执行

 

This works, but there is a bug

这有效,但是有一个错误

 If the call to os.Create fails, the function will return without closing the source file.

如果调用 os.Create 失败,函数将返回而不关闭源文件。

This can be easily remedied by putting a call to src.// remedied 补救,纠正

这可以通过调用 src 轻松解决

Defer statements allow us to think about closing each file right after opening it, guaranteeing that, regardless of the number of return statements in the function, the files will be closed.

//guarantee 确保 保证 ,无论函数中的有多少数量的返回语句,文件都会被关闭

中文(简体)

Defer 语句允许我们考虑在打开每个文件后立即关闭它,保证无论函数中的 return 语句数量如何,文件都将被关闭。

. A deferred function's arguments are evaluated when the defer statement is evaluated.

//arguments 参数   evaluate 评估,求值,衡量

中文(简体)

  1. defer 语句被评估时,延迟函数的参数被评估。
  2. Expression表达式
  3. In this example, the expression "i" is evaluated when the Println call is deferred. The deferred call will print "0" after the function returns.
  4. func a() {
  5. i := 0
  6. defer fmt.Println(i)
  7. i++
  8. return
  9. }

 

  1. 2. Deferred function calls are executed in Last In First Out order after the surrounding function returns.  // Last In First Out order后进先出的顺序
  2. This function prints "3210":
  3. func b() {
  4. for i := 0; i < 4; i++ {
  5. defer fmt.Print(i)
  6. }
  7. }

Deferred functions may read and assign to the returning function's named return values.

 

中文(简体)

延迟函数可以读取并分配给返回函数的命名返回值。

In this example, a deferred function increments the return value i after the surrounding function returns. Thus, this function returns 2:

func c() (i int) {

    defer func() { i++ }()

    return 1

}

This is convenient for modifying the error return value of a function; we will see an example of this shortly.

 

中文(简体)

这样方便修改函数的错误返回值;我们很快就会看到一个例子。

https://blog.go-zh.org/defer-panic-and-recover

关于延迟,中断和恢复go操作的网址

英文太难看懂了,之前太懒了没长期训练

 

 

 

 

P=&i

使得P指向i

*p=xx

则重定向改变了i的值

 

Win+d返回桌面hhh(好用)

Win+I window设置

ctrl+alt+l格式化代码

Ctrl+F查找

Goland;Alt+F12 到终端

 

 

 

Reformat code格式化代码

 

//一个结构体(struct)就是一组字段(field)。

线程(Thread)定义:操作系统能够进行运算调度的最小单位

类比理解

线程-流水线

CPU核心-工人

Concurrency

谷歌大会上有个 视频是Go concurrency patterns

 

Google I/O 2012 - Go Concurrency Patterns

https://www.youtube.com/watch?v=f6kdp27TYZs&t=610s

讲的是go的特别并发设计之类的

然后就去了解了

Difference between Concurrency and Parallelism

 

 

 

对每个任务分别分配时间快速切换

有不同任务的优先级

Cpu核心在多个线程之间快速切换

分配时间非常短,切换非常快

所以才感觉多个任务在同时进行

并发;对单个cpu核心来说的

双核,四核,六核;

并行处理任务

多核cpu同时处理指令:并行

 

//切片就像数组的引用
//切片并不存储任何数据,它只是描述了底层数组中的一段。
//更改切片的元素会修改其底层数组中对应的元素。
//与它共享底层数组的切片都会观测到这些修改。

 

Redundant 冗余的多余的累赘的

将字符串数组(或 slice)连接起来可以通过 Join 实现,函数签名如下:

func Join(a []string, sep string) string


//切片是对底层数组中的一段的描述
//更改切片元素会修改其底层数组中的对应元素

 

Join concatenates the elements of its first argument to create a single string. The separator string sep is placed between elements in the resulting string.

 

oin 连接其第一个参数的元素以创建单个字符串。分隔符字符串 sep 放置在结果字符串中的元素之间

 

func make([]T, len, cap) []T

其中T代表被创建的切片元素的类型。函数 make 接受一个类型、一个长度和一个可选的容量参数。 调用 make 时,内部会分配一个数组,然后返回数组对应的切片。

 

 切片的本质:

 

https://blog.go-zh.org/go-slices-usage-and-internals

 

 

切片的内幕

一个切片是一个数组片段的描述。它包含了指向数组的指针,片段的长度, 和容量(片段的最

 

大长度)。

 

 

 

Range

for 循环的 range 形式可遍历切片或映射。

当使用 for 循环遍历切片时,每次迭代都会返回两个值。第一个值为当前元素的下标,第二个值为该下标所对应元素的一份副本。

 

 

把注释都贴近代码里了,方便看,就不整理在这里了

 

 

 

posted @   Fantastic-Marmot  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示