Cloud Native Foundation - Go Sync
线程安全
锁
Go 不仅提供了基于 CSP 的通讯模型,也支持基于共享内存的多线程数据访问
sync 包提供了锁的基本原语
同步工具
用途
sync.Mutex
互斥锁:Lock加锁,Unlock解锁
sync.RWMutex
读写分离锁:不限制并发读,只限制并发写和并发读写
sync.WaitGroup
等待一组 goroutine 返回,类似于 Java 的 CountDownLatch
sync.Once
保证某段代码只执行1次,典型场景:单例模式
sync.Cond
让一组 goroutine 在满足特定条件时被唤醒,典型场景:生产者消费者模型
Mutex12345678910111213141516171819202122232425262728293031323334353637func unsafeWrite() { // fatal error: concurrent map writes conflictMap := map[int]int{} for i := 0; i < 100; i++ { ...
Cloud Native Foundation - Go Features
特点
可以高效编译、支持高并发、面向垃圾回收
秒级完成大型程序的单节点编译
依赖管理清晰
不支持继承
支持垃圾回收、支持并发执行、支持多线程通讯
对多核计算机支持友好
特性来源
环境变量
Env
Desc
Value
GOARCH
The architecture, or processor, for which to compile code.
amd64
GOBIN
The directory where ‘go install’ will install a command.
GOCACHE
The directory where the go command will store cached information for reuse in future builds.
~/Library/Caches/go-build
GOMODCACHE
The directory where the go command will store downloaded modules.
~/go/pkg/mod
GO ...
Go Paradigm - Visitor
概述
Visitor 模式是将算法和结构分离的一种方法
在不修改结构的情况下,向现有对象结构添加新操作,遵循 OCP
不同 Visitor 用来访问同一个数据结构的不同部分
示例 1
类似于 Java java.util.function.Consumer
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253type Visitor func(shape Shape)type Shape interface { accept(Visitor)}type Circle struct { Radius int}func (c *Circle) accept(v Visitor) { v(c)}type Rectangle struct { Width, Height int}func (r *Rectangle) accept(v Visitor) { ...
Go Paradigm - Pipeline
HTTP1234567891011121314151617181920212223242526272829303132333435363738type HttpHandlerDecorator func(http.HandlerFunc) http.HandlerFuncfunc Handler(h http.HandlerFunc, decors ...HttpHandlerDecorator) http.HandlerFunc { for i := range decors { h = decors[len(decors)-1-i](h) } return h}func WithServerHeader(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { log.Println("--->WithServerHeader") ...
Go Paradigm - Decorator
示例示例 11234567891011121314151617181920func decorator(f func(s string)) func(s string) { return func(s string) { fmt.Println("Started") f(s) fmt.Println("Done") }}func Hello(s string) { fmt.Println(s)}func main() { decorator(Hello)("Hello, World!")}// output:// Started// Hello, World!// Done
示例 2123456789101112131415161718192021222324252627type SumFunc func(int64, int64) int64func getFunctionN ...
Go Paradigm - Generation
代码生成
Go 的代码生成主要用来解决泛型编程的问题
泛型编程:类型与算法解耦
类型检查
Go 在支持泛型之前,只能用 interface{},必然涉及到类型检查
Type Assertion1234567891011121314151617181920212223type Container []interface{}func (c *Container) Put(elem interface{}) { *c = append(*c, elem)}func (c *Container) Get() interface{} { elem := (*c)[0] *c = (*c)[1:] return elem}func main() { c := &Container{} c.Put(1) c.Put("hello") elem, ok := c.Get().(int) // t ...
Go Paradigm - MapReduce
IntroductionMap1234567891011121314151617181920212223242526272829func MapStrToStr(arr []string, fn func(s string) string) []string { var sl []string for _, s := range arr { sl = append(sl, fn(s)) } return sl}func MapStrToInt(arr []string, fn func(s string) int) []int { var sl []int for _, s := range arr { sl = append(sl, fn(s)) } return sl}func main() { list := []string{"hello", "world"} x : ...
Go Paradigm - Delegation + IoC
嵌入委托123456789101112131415161718192021222324252627package maintype Widget struct { X, Y int}type Label struct { Widget // type embedding - delegation Text string // aggregation}type Button struct { Label // type embedding - delegation}type ListBox struct { Widget // type embedding - delegation Texts []string // aggregation Index int // aggregation}func main() { label := Label{Widget{10, 10}, "Sta ...
Go Paradigm - Functional Options
配置问题12345678type Server struct { Addr string Port int Protocol string Timeout time.Duration MaxConns int TLS *tls.Config}
Go 不支持函数重载,需要使用不同的函数名来应对不同的配置选项
123456789101112131415func NewDefaultServer(addr string, port int) (*Server, error) { return &Server{addr, port, "tcp", 30 * time.Second, 100, nil}, nil}func NewTLSServer(addr string, port int, tls *tls.Config) (*Server, error) { return &Server{addr, port, " ...
Cloud Native Foundation - Overview
基本概念
在包括公有云、私有云、混合云等动态环境中构建和运行规模化应用的能力
云原生是一种思想,是技术、企业管理方法的集合
技术层面
应用程序从设计之初就为在云上运行而做好准备
云平台基于自动化体系
流程层面
基于 DevOps CI/CD
基于多种手段
应用容器化封装
服务网格
不可变基础设施
声明式API - 核心 + 标准
意义
提升系统的适应性、可管理性、可观测性
使工程师能以最小成本进行频繁和可预测的系统变更
提升速度和效率,助力业务成长,缩短I2M(Idea to Market)
核心项目