GopherChina 2020 Go Programming Patterns 学习笔记篇2

本篇继续学习左耳朵耗子老师的Go Programming Patterns下半部分,PPT太长了,70多页。

Topic 10 函数化的选项配置

由于Golang不允许使用同一个名字来命名函数,必须是不同的名称,即使参数不同,这与Java不一样,java的方法签名是包含参数的。所以遇到那种多个参数来实例化一个变量的,就会比较麻烦。会有类似代码产生:

type Server struct {
	Addr string
	Port int
	Protocol string 
	Timeout time.Duration
	MaxConns int
	TLS *tls.Config 
}

func NewServer(addr string, port int) (*Server, error) {
//...
}
func NewTLSServer(addr string, port int, tls *tls.Config) (*Server, error) {
//...
}
func NewServerWithTimeout(addr string, port int, timeout time.Duration) (*Server, error) { //...
}
 
func NewTLSServerWithMaxConnAndTimeout(addr string, port int, maxconns int, timeout time.Duration, tls *tls.Config) (*Server, error) {
//...
}

然后就有一种,使用专用配置结构体来优化这种实例化函数。

type Config struct { Protocol string
	Timeout time.Duration
	Maxconns int
	TLS *tls.Config
}

type Server struct {
	Addr string
	Port int
	Conf *Config
}

func NewServer(addr string, port int, conf *Config) (*Server, error) {
	//...
}

//Using the default configuratrion
srv1, _ := NewServer("localhost", 9000, nil)

conf := ServerConfig{Protocol:"tcp", Timeout: 60*time.Duration} 
srv2, _ := NewServer("locahost", 9000, &conf)
}

这样写有什么问题,这样有一个问题是零值或者nil值的处理,即在NewServer里需要处理这些零值,比如MaxConn要处理是否为0,TLS是否为nil,需要挨个判断,然后处理。然后就有了这种函数化参数的令人眼前一亮的操作。

type Option func(*Server)
func Protocol(p string) Option {
    return func(s *Server) {
        s.Protocol = p
    }
}
func Timeout(timeout time.Duration) Option {
	return func(s *Server) {
        s.Timeout = timeout
    }
}
func MaxConns(maxconns int) Option {
    return func(s *Server) {
        s.MaxConns = maxconns
    }
}
func TLS(tls *tls.Config) Option {
    return func(s *Server) {
		s.TLS = tls 
	}
}


func NewServer(addr string, port int, options ...func(*Server)) (*Server, error) {
   srv := Server{
        Addr:
        Port:
        Protocol: "tcp",
        Timeout:  30 * time.Second,
        MaxConns: 1000,
        TLS:      nil,
    }
    for _, option := range options {
        option(&srv)
    }
	//...
    return &srv, nil
}

// 使用
s1, _ := NewServer("localhost", 1024)
s2, _ := NewServer("localhost", 2048, Protocol("udp"))
s3, _ := NewServer("0.0.0.0", 8080, Timeout(300*time.Second), MaxConns(1000))

乍一看,有点懵逼是吧。就是把所有的参数,使用了函数来处理,并且返回的是个函数,他通过把参数传递给了要返回的函数。要返回的函数干了啥呢?它把传入的参数,设置到Server这个要初始化的实例上,这样就可以达到在初始化中直接挨个执行这些func,传入sever实例,即可完成初始化。只是我觉得这样做,不如直接给Server写参数函数,直接设置参数值不就好了吗?令人疑惑

 	func(s *Server) {
        s.Timeout = timeout
    }

Topic 11 Map/Reduce/Filter.

高阶函数

Go Design Patterns by Mario Castro Contreras English | 6 Mar. 2017 | ISBN: 1786466201 | 399 Pages | EPUB/PDF (conv) | 3.11 MB Key Features Introduction of the CSP concurrency model by explaining GoRoutines and channels. Objective comparison, with the help of images, of the CSP concurrency model and the Actor model to give the audience a "side by side" understanding of the CSP model. Explanation, including comprehensive text and examples, of all known GoF design patterns in Go. Book Description Go is a multi-paradigm programming language that has built-in facilities to create concurrent applications. Design patterns allow developers to efficiently address common problems faced during developing applications. Go Design Patterns will provide readers with a reference point to software design patterns and CSP concurrency design patterns to help them build applications in a more idiomatic, robust, and convenient way in Go. The book starts with a brief introduction to Go programming essentials and quickly moves on to explain the idea behind the creation of design patterns and how they appeared in the 90's as a common "language" between developers to solve common tasks in object-oriented programming languages. You will then learn how to apply the 23 GoF design patterns in Go and also learn about CSP concurrency patterns, the "killer feature" in Go that has helped Google develop software to maintain thousands of servers. Thus the book will enable you to understand and apply design patterns in an idiomatic way that will produce concise, readable, and maintainable software. What you will learn All basic syntax and tools needed to start coding in Go. Encapsulate the creation of complex objects in an idiomatic way in Go. Create unique instances that cannot be duplicated within a program. Understand the importance of object encapsulation to provide clarity and maintainability. Prepare cost-effective actions so that different parts of the program aren't affected by expens
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值