GO全部意思

Python014

GO全部意思,第1张

全部意思如下:

英文单词:go

翻译中文意思|用法|音标|

go: [ gəu ]

v. 去,走

[计算机] 执行

词形变化:

名词复数:goes 动词过去式:went 过去分词:gone 现在分词:going 第三人称单数:goes

例句与用法:

1. How are things going?

事情进行得如何?

2. I must be going.

我该走了。

3. Where does this road go to?

这条路通向哪?

4. The money will go to finance a new community centre.

这些钱将用来资助一个新社区中心。

5. She's sixteen, going on seventeen.

她十六岁,快十七岁了。

6. It's all go in the office today.

今天办公室里忙得不可开交。

7. No one may leave the classroom until the bell goes.

铃响以前任何人均不得离开教室。

8. I'll have a go at mending your bike today.

我今天修修你的自行车。

golang在1.6.2的时候还没有自己的context,在1.7的版本中就把golang.org/x/net/context包被加入到了官方的库中。中文译作“上下文”,它主要包含了goroutine 的运行状态、环境等信息。

context 主要用来在 goroutine 之间传递上下文信息,包括:同步信号、超时时间、截止时间、请求相关值等。

该接口定义了四个需要实现的方法:

如果有个网络请求Request,然后这个请求又可以开启多个goroutine做一些事情,当这个网络请求出现异常和超时时,这个请求结束了,这时候就可以通过context来跟踪这些goroutine,并且通过Context来取消他们,然后系统才可回收所占用的资源。

为了更方便的创建Context,包里头定义了Background来作为所有Context的根,它是一个emptyCtx的实例。

Background返回一个非空的Context。它永远不会被取消。它通常用来初始化和测试使用,作为一个顶层的context,也就是说一般我们创建的context都是基于Background。

TODO返回一个非空的Context。当不清楚要使用哪个上下文的时候可以使用TODO。

他们两个本质上都是emptyCtx结构体类型,是一个不可取消,没有设置截止时间,没有携带任何值的Context。

有了如上的根Context,那么是如何衍生更多的子Context的呢?这就要靠context包为我们提供的With系列的函数了。

通过这些函数,就创建了一颗Context树,树的每个节点都可以有任意多个子节点,节点层级可以有任意多个。

WithCancel函数,最常用的派生 context 方法。该方法接受一个父 context。父 context 可以是一个 background context 或其他 context。

WithDeadline函数,该方法会创建一个带有 deadline 的 context。当 deadline 到期后,该 context 以及该 context 的可能子 context 会受到 cancel 通知。另外,如果 deadline 前调用 cancelFunc 则会提前发送取消通知。

WithTimeout和WithDeadline基本上一样,这个表示是超时自动取消,是多少时间后自动取消Context的意思。

WithValue函数和取消Context无关,它是为了生成一个绑定了一个键值对数据的Context,这个绑定的数据可以通过Context.Value方法访问到,一般我们想要通过上下文来传递数据时,可以通过这个方法,如我们需要tarce追踪系统调用栈的时候。

使用Context的程序应遵循以下规则,以使各个包之间的接口保持一致:

1.不要将 Context 塞到结构体里。直接将 Context 类型作为函数的第一参数,而且一般都命名为 ctx。

2.不要向函数传入一个 nil 的 context,如果你实在不知道传什么,标准库给你准备好了一个 context:todo。

3.不要把本应该作为函数参数的类型塞到 context 中,context 存储的应该是一些共同的数据。例如:登陆的 session、cookie 等。

4.同一个 context 可能会被传递到多个 goroutine,别担心,context 是并发安全的。

注:本文是对 golang-101-hacks 中文翻译。

在Go语言中,函数参数是值传递。使用slice作为函数参数时,函数获取到的是slice的副本:一个指针,指向底层数组的起始地址,同时带有slice的长度和容量。既然各位熟知数据存储的内存的地址,现在可以对切片数据进行修改。让我们看看下面的例子:

In Go, the function parameters are passed by value. With respect to use slice as a function argument, that means the function will get the copies of the slice: a pointer which points to the starting address of the underlying array, accompanied by the length and capacity of the slice. Oh boy! Since you know the address of the memory which is used to store the data, you can tweak the slice now. Let's see the following example:

运行结果如下

由此可见,执行modifyValue函数,切片s的元素发生了变化。尽管modifyValue函数只是操作slice的副本,但是任然改变了切片的数据元素,看另一个例子:

You can see, after running modifyValue function, the content of slice s is changed. Although the modifyValue function just gets a copy of the memory address of slice's underlying array, it is enough!

See another example:

The result is like this:

而这一次,addValue函数并没有修改main函数中的切片s的元素。这是因为它只是操作切片s的副本,而不是切片s本身。所以如果真的想让函数改变切片的内容,可以传递切片的地址:

This time, the addValue function doesn't take effect on the s slice in main function. That's because it just manipulate the copy of the s, not the "real" s.

So if you really want the function to change the content of a slice, you can pass the address of the slice:

运行结果如下