An open API service indexing awesome lists of open source software.

https://github.com/navono/golangsimpletutorial

A sample source demonstrate Golang
https://github.com/navono/golangsimpletutorial

golang

Last synced: over 1 year ago
JSON representation

A sample source demonstrate Golang

Awesome Lists containing this project

README

          

# GolangSimpleTutorial
![](https://img.shields.io/badge/language-Golang-orange.svg)
[![](https://img.shields.io/badge/license-MIT-000000.svg)](https://github.com/navono/golangSimpleTutorial.git/blob/master/LICENSE)

# 环境配置
`VS Code`下安装`Go`插件后,提示需要安装一些工具,但是在墙内会经常导致失败。可以[在此](https://pan.baidu.com/s/1mjLPKfe)下载已经编译好的`Windows`版本。将这些二进制文件放置到`GOPATH\bin`目录下即可。

# 运行
> go run main.go

# 解释说明
## goroutine
`goroutine`是什么?是`OS线程`?它是如何工作的?我们可以随意创建任意数量吗?
> `goroutine`是`Go`特有的,它们不是`OS线程`,或者说连线程都不是。

它们由`Go`的`runtime`统一管理,一种高层级的抽象的`coroutine`。`coroutine`是并发的`subroutine`(functions, closures or methods in Go),同时是不可抢占的(也就是不能被中断),相反,`coroutine`是可重入(reentry)和挂起的(suspension)。

`goroutine`不支持可重入和挂起,这些统一由`Go`的`runtime`管理。`runtime`观察`goroutine`的行为,在`goroutine`阻塞时,对其挂起,非阻塞时,恢复其运行。

由此看出,`goroutine`是一种特殊的`coroutine`。

`goroutine`是隐式并发的。但是这并不是说并发是`coroutine`的一个属性。还是需要有个宿主(Host)来同时管理多个`coroutine`,然后对其进行调度。

`Go`的并发采用的是`fork-join`模型。

## Go的并发哲学
Go除了提供了goroutine之外,也提供了内存访问同步原语机制。那么在这两者之间该如何选择?回答以下四个问题:
1. 是否尝试转移数据的所有权?
> Yes -> Channels

No -> Primitives
2. 是否尝试保护结构体的内在状态?
> Yes -> Primitives

No -> Channels

例如:
```go
type Counter struct {
mu sync.Mutex
value int
}
func (c *Counter) Increment() {
c.mu.Lock()
defer c.mu.Unlock()
c.value++
}
```
3. 是否在多个逻辑代码中进行协调配合?
> Yes -> Channels

No -> Primitives
4. 代码是否是性能关键区(performance-critical section)
> Yes -> Primitives

No -> Channels

旨在简化,尽量使用Channel。

# 删除临时文件
> go clean -i