从Python到Go:初学笔记

【自取】最近整理的,有需要可以领取学习:

本文记录了围棋学习过程中的一些笔记,主要比较了Python和围棋的不同之处,并进行了简要的描述,使Python程序员对围棋语言的特点有了一个简单的了解。初学者难免会犯错。欢迎您批评、更正、补充、交流。谢谢。

[En]

This article records some notes on the process of learning Go, mainly comparing the differences between Python and Go and making a brief description, so that Python programmers have a brief understanding of the features of Go language. It is inevitable that there will be mistakes in beginners. You are welcome to criticize, correct, supplement and exchange. Thank you.

Go中的数组需要在创建时确定长度,一个更灵活的对象是slice,后者可以使用append添加,两者的定义方式相似。

var StrArray [10]string //数组,长度为10
var StrSlice []string //slice

slice可以根据现有的数组(称为底层数组)创建,但对其的修改会导致底层数组的改变。

Go语言支持指针,用法和C一样

结构体和Python中的Class相似,但在这一代码段中只能定义类型的数据布局,方法需要定义指定接收对象的函数(见”方法”)。

type Point struct{
    X int
    Y int
}

结构体嵌套和匿名成员

将结构成员添加到结构中会使访问变量变得很麻烦。在围棋中,结构成员可以在没有名称的情况下定义为匿名成员。

[En]

Adding structure members to structures makes it troublesome to access variables. In Go, structure members can be defined without names as anonymous members.

结合匿名成员和方法对匿名成员的处理(一个结构包含一个结构的匿名成员可以接收该结构),匿名成员机制可以被视为继承

[En]

Combined with anonymous members and methods’ handling of anonymous members (a structure that contains anonymous members of a structure can receive the structure), * anonymous member mechanism can be regarded as inheritance *

type ColoredPoint struct {
    Point // 匿名成员
    color string
}

var cp ColoredPoint
cp.X = 1
cp.Y = 2
cp.color = "red"

与普通函数不同,该方法指定接收对象。

[En]

Unlike ordinary functions, the method specifies the receiving object.

包含结构的匿名成员的结构可以接收该结构的方法。

[En]

The method that a structure that contains anonymous members of a structure can receive that structure.

定义与实现

隐式实现:满足接口的方法是在不显式声明的情况下实现接口

[En]

Implicit implementation: the way to satisfy an interface is to implement an interface without explicitly declaring

type Phone interface {
    call()()
    text(str []string)(n int)
}

当一个类型具有它所属的输入和输出的WRITE方法时,它被称为实现了Writer接口。

[En]

When a type has Write methods for the inputs and outputs to which it belongs, it is said to implement the Writer interface.

type iPhone struct{}

func (p iPhone) call (){
    fmt.Println("call from iPhone")
}

func (p iPhone) text (str []string){
    fmt.Println(str)
    fmt.Println("text from iPhone")
    return len(str)
}

接口的应用

可以将接口定义为变量,并可以为其分配特定类型。

[En]

The interface can be defined as a variable and can be assigned a specific type.

var phone Phone

// 赋值方法一
var iphone iPhone
phone = iphone
phone.call()
phone.text("test")

// 赋值方法二
phone = new(iPhone)
phone.call()
phone.text("test")

goroutine

Go中每一个并发的活动称为goroutine,不同于Python虚假的多线程或不稳定的多进程,goroutine被归类为协程(Coroutine)。

go f()

不同于Python会自动等待各Process运行结束后退出,在Go中main函数返回时,所有的goroutine都暴力地终结,可以使用下文提及的通道阻塞或者sync的WaitGroup等待以保证各goroutine运行。

通道用于goroutine间的通信,不同于Python的Threading库或multiporcessing库中的Queue(队列),Go中的通道是需要标注数据类型的。

ch := make(chan int) //定义通道,int为数据类型
ch

通道的发送和接收操作都被阻塞。

[En]

The sending and receiving operations of the channel are all blocked.

不同于Queue关闭后无法收发,通道关闭后无法发送,但可以接收剩余的数据。

ch1 := make(chan int)
ch2 := make(chan int, 0)
// 两者含义相同

上面定义的信道是无缓冲的信道,即在非阻塞传输之后,在接收数据之前阻塞第二次传输。

[En]

The channel defined above is an unbuffered channel, that is, after a non-blocking transmission, the second transmission is blocked before the data is received.

ch := make(chan int, 3) //定义通道,int为数据类型,容量为3

如上所述,该信道可以被发送四次而不被阻塞,并且第五次传输被阻塞(不接收)。

[En]

As defined above, the channel can be sent four times without blocking, and the fifth transmission is blocked (without receiving).

为了避免误用,在定义函数的参数时可以固定通道的方向。

[En]

In order to avoid misuse, the direction of the channel can be fixed when defining the parameters of the function.

func f(in

如上所述,通道输入是仅可针对函数f接收的通道,而通道输出是仅可针对函数f发送的通道。

[En]

As defined above, channel in is a channel that can only be received for function f, and channel out is a channel that can only be sent for function f.

select多路复用

select的类似于switch,但不同的是select的分支上是阻塞着的操作而非数据。select使可以同时等待多个操作的阻塞,直到某一个分支上的操作不再阻塞。每个select只执行一个分支。

select {
case x1

共享变量

任何涉及并发性的编程都应该遵循这样一句话:

[En]

A sentence that any programming involving concurrency should follow:

”Do not communicate by sharing memory; instead, share memory by communicating.”

不要通过共享内存进行通信,而要使用通信来共享内存。也就是说,对象应该被限制为在它们被顺序执行的环境中写入操作,例如在协作程序中。

[En]

Don’t communicate through shared memory, use communication to share memory. That is, objects should be restricted to write operations in an environment in which they are executed sequentially, such as in a collaborative program.

也可以用锁。

类似multiprocessing.Lock有acquire()和release(),sync.Mutex有Lock()和Unlock()。(记得用defer延迟执行Unlock()以保证解锁的执行)

Go提供共了一种更复杂的锁,除了不可并行的写锁Lock()和Unlock(),还有可并行的读锁RLock()和RUnlock()。其使用类似于数据库的二、三级封锁协议。

延迟初始化,Once函数以某个函数为参数,保证这个 只需要执行一次的函数在并行情况下 执行且只执行一次。相同效果虽然用RWMutex也可以实现但Once更加简便

输出一个包含所有数据竞争的报告,可以通过在运行/构建/测试时添加-race来使用该报告。

[En]

Output a report containing all the data races, which can be used by adding-race when go run/build/test.

GOMAXPROCS

确定要使用的OS线程的数量可以设置为环境变量,也可以由函数runtime.GOMAXPROCS控制。

[En]

Determining the number of OS threads to use can be set as an environment variable or controlled by the function runtime.GOMAXPROCS.

《Go程序设计语言》

Original: https://www.cnblogs.com/yc0806/p/15128953.html
Author: 多事鬼间人
Title: 从Python到Go:初学笔记

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/7277/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部