A lecture of Go
Contents
概述
串讲,是以会议形式,串讲人给同事讲一个topic。会上的其他同事,对串讲人进行相关知识的提问,以考查其对此topic的掌握程度。便于尽快上手,投入工作。
本文是一篇Go语言的串讲文档,所包含的知识点重在日常工作使用,不会深入研究底层的实现细节。
Go语言是一种type语言,可以这样说,everything in Golang is a type。
Go语言中的Type
总的来说,Go中包含以下三种type
- built-in type. 具体有numeric, string, boolean
- reference type. 具体有 slice, map, interface, channel, function types
- struct type.
built-in type
numeric
integer
type size (bits) int8 8 int16 16 int32 32 int64 64 int Platform dependent float, float32和float64
complex
byte 和 rune
我们习惯称的
byte是 uint8的别称,rune是int32的别称string
实际上string也是reference类型,底层是一个连续的固定字节序列。1 2var name = "Hello" var location = `Shanghai`boolean
跟其他编程语言一样
trueandfalseconstants
1const PI = 3.141592array
1var num [3]intGo里的array跟C、C++、Java里的array为引用类型不同,其为value类型。这个要注意⚠️
reference type
slice
[]T含有同类型可变长度的序列 。slice的底层实现是数组,含有cap,len属性。
1weeks := []string{"Mon","Tue","Wed","Thr","Fri","Sta", "Sun"}map
map[K]V就是我们常用的key⇒value 关联无序数组,
map[K]V。但是与PHP的关联数组不一样,在Go中,一个map里的所有key的类型一致,所有vlaue的类型也一致。是reference类型。1age := make(map[string]int)channel
channel是goroutine之前进行通信的桥梁,分为unbuffed和buffered两种。具有send,receive,close操作。1done := make(chan struct{}{})interface
function type
struct type
由其他类型的字段组合成的结构体
| |
结构体比较,如果两个结构体里的所有字段都可以比较,那么这两个结构体则可以比较。
结构体可以嵌套,并且可以使用匿名字段。
package
A package is a collection of source files in the same directory that are compiled together
module
A repository contains one or more modules. A module is a collection of related Go packages that are released together. A Go repository typically contains only one module, located at the root of the repository
function
function是重复利用代码的优秀实践。
| |
defer, panic and recover
| |
method
method是一个具体的Object拥有的behavior。
| |
interface
interface是一系列行为的集合,一个具体类型实现了接口的所有行为,则具体类型属于这个接口类型
| |
OOP in Go
封装:小写字母开头的变量,函数,只有本package可见。如果将package里的字段,变量和函数向外暴露,则大写字母开头。
继承:not supported!
多态: 通过interface,可以很优雅地实现多态。
goroutine
Goroutine是由Go runtime管理的轻量线程。
go 关键字开启一个goroutine
| |
Go有如下两种并发方式:
- Go提倡的 communicating sequential processes. use
channelto communicate - 传统的 shared memory multithreading. use
mutal exclusion
channel
You can use channel to communicate between goroutine.
| |
unbuffered
By default, sends and receives block until the other side is ready.
1ch := make(chan int)buffered
Sends to a buffered channel block only when the buffer is full. Receives block when the buffer is empty
1ch := make(chan int, 10) // buffered channel
Questions
What’s the difference between
makeandnew?makeis a built-in function, which only worked on slice, map and channel, return a regular values(slice, map or channel).newonly returns pointers to initialised memory.What will happened when two or more goroutine receive values on a unbuffered channel?
Just like one receiving goroutine, multiple goroutine receive values on the same channel will block.
Which type is concurrency safe?
As I know, channel is concurrency safe. Others primitive type like slice, map, are not.
What is concurrency safe and how to make a type concurrency safe?
- not to write the variable
- avoid accessing the variable from multiple goroutines. If needed, can use channel to update variable between multiple goroutine
- allow many goroutine to access the variable, but only one at a time. This approach is known as
mutual exclusion
Reference
Author Paisen
LastMod 2022-05-29