

Golang 的一些编程思维和思想,以及总结一些常见的优雅编程实战技巧。
作者:allendbwu,腾讯 PCG 后台开发工程师
首先,我们先来看下最基本的,就是 Golang 的学习技巧,比如通读 Golang 的一些好的文章:
要通读 golang 官方的编码规范,主要是要参考官方的 CodeReviewComments 和 Effective Go 这两篇官方文章,真的非常推荐必须要好好的看完、看懂这两篇文章(英文不好的同学可以看中文翻译文档),然后按照官方编码规范来具体 coding,主要是能够在具体的编码中有迹可循。
参考业界大牛们的代码,主要是看一些开源的优质的项目,比如 Google 他们这帮人自己搞的 Kubernetes、Istio,还有一些好的项目如 Docker、CoreDNS、etcd 等等。
然后就是实践,实实在在的跑一些代码示例,可以自己建立一个 base-code 的项目,里面就是你的各种示例,然后进行一些修改、执行。具体的代码示例可以从官方文档上来,推荐Go by Example,里面有大量非常好的例子。也可以自己网上随便搜下,重要的自己要修改并执行,查看和分析结果:Go 101
其次,要理解 Golang 编程思维,首先要理解 Golang 这门语言的创始初衷,初衷就是为了解决好 Google 内部大规模高并发服务的问题,主要核心就是围绕高并发来开展;并且同时又不想引入面向对象那种很复杂的继承关系。首先,就是可以方便的解决好并发问题(包括高并发),那么就需要有并发思维,能够并发处理就通过并发来进行任务分配
再者,面向对象编程思想,利用好 interface、 struct 来实现继承、多态的用法:
然后,理解 Golang 语言本身的一些特性: - 强类型,语法上要注意处理;- GC,实际中要观察 GC 日志并分析;- 注意语法语义尽可能的简单、保持各种类型定义尽可能精简。
最后,从 Golang 社区的一些最佳实践来看,Golang 的各种组件需要尽可能的精简。
/*
一个更为优雅的构造函数的实现方式
参考:
https://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html
通过这个方式可以方便构造不同对象,同时避免了大量重复代码
*/
package main
import (
"fmt"
"time"
"golang.org/x/net/context"
)
type Cluster struct {
opts options
}
type options struct {
connectionTimeout time.Duration
readTimeout time.Duration
writeTimeout time.Duration
logError func(ctx context.Context, err error)
}
// 通过一个选项实现为一个函数指针来达到一个目的:设置选项中的数据的状态
// Golang函数指针的用法
type Option func(c *options)
// 设置某个参数的一个具体实现,用到了闭包的用法。
// 不仅仅只是设置而采用闭包的目的是为了更为优化,更好用,对用户更友好
func LogError(f func(ctx context.Context, err error)) Option {
return func(opts *options) {
opts.logError = f
}
}
// 对关键数据变量的赋值采用一个方法来实现而不是直接设置
func ConnectionTimeout(d time.Duration) Option {
return func(opts *options) {
opts.connectionTimeout = d
}
}
func WriteTimeout(d time.Duration) Option {
return func(opts *options) {
opts.writeTimeout = d
}
}
func ReadTimeout(d time.Duration) Option {
return func(opts *options) {
opts.readTimeout = d
}
}
// 构造函数具体实现,传入相关Option,new一个对象并赋值
// 如果参数很多,也不需要传入很多参数,只需要传入opts ...Option即可
func NewCluster(opts ...Option) *Cluster {
clusterOpts := options{}
for _, opt := range opts {
// 函数指针的赋值调用
opt(&clusterOpts)
}
cluster := new(Cluster)
cluster.opts = clusterOpts
return cluster
}
func main() {
// 前期储备,设定相关参数
commonsOpts := []Option{
ConnectionTimeout(1 * time.Second),
ReadTimeout(2 * time.Second),
WriteTimeout(3 * time.Second),
LogError(func(ctx context.Context, err error) {
}),
}
// 终极操作,构造函数
cluster := NewCluster(commonsOpts...)
// 测试验证
fmt.Println(cluster.opts.connectionTimeout)
fmt.Println(cluster.opts.writeTimeout)
}除了构造函数这个思想之外,还有一个思想,就是我们要善于利用 struct 封装对象方法,然后再 new 一个对象出来,如下:
type Cluster struct {
opts options
}
func NewCluster(opts ...Option) *Cluster {
....
cluster := new(Cluster)
cluster.opts = clusterOpts
return cluster
}Golang 里面没有 C++ 、Java 那种继承的实现方式,但是,我们可以通过 Golang 的匿名组合来实现继承,这里要注意,这个是实际编程中经常用到的一种姿势。具体实现就是一个 struct 里面包含一个匿名的 struct,也就是通过匿名组合,这最基础的基类就是一个 struct 结构,然后定义相关成员变量,然后再定义一个子类,也是一个 struct,里面包含前面的 struct,即可实现继承。
示例代码如下,代码里面有详细的解释:
package main
import (
"fmt"
)
// 【基类】
//定义一个最基础的struct类MsgModel,里面包含一个成员变量msgId
type MsgModel struct {
msgId int
msgType int
}
// MsgModel的一个成员方法,用来设置msgId
func (msg *MsgModel) SetId(msgId int) {
msg.msgId = msgId
}
func (msg *MsgModel) SetType(msgType int) {
msg.msgType = msgType
}
//【子类】
// 再定义一个struct为GroupMsgModel,包含了MsgModel,即组合,但是并没有给定MsgModel任何名字,因此是匿名组合
type GroupMsgModel struct {
MsgModel
// 如果子类也包含一个基类的一样的成员变量,那么通过子类设置和获取得到的变量都是基类的
msgId int
}
func (group *GroupMsgModel) GetId() int {
return group.msgId
}
/*
func (group *GroupMsgModel) SetId(msgId int) {
group.msgId = msgId
}
*/
func main() {
group := &GroupMsgModel{}
group.SetId(123)
group.SetType(1)
fmt.Println("group.msgId =", group.msgId, "\tgroup.MsgModel.msgId =", group.MsgModel.msgId)
fmt.Println("group.msgType =", group.msgType, "\tgroup.MsgModel.msgType =", group.MsgModel.msgType)
}面向对象编程中,我们很多情况下,都会定义一个虚基类,然后利用多态去实现各种相似的场景或者说任务。
Golang 里面可以通过 interface + struct 来实现虚基类的用法。interface 用来定义一个 "虚基类",然后一个 struct 结构定义,用来实现这个 interface 中定义的方法,并且可以有多个类似的 struct 来实现这个 interface,只要实现了这个 interface 中定义的方法即可。这也是典型的多态的一种编程思想,也就是说 Golang 通过接口去实现了多态。
具体流程如下,这个是我实际项目(大型 IM 架构)中的实现方式:
在一个项目工程中,为了使得代码更优雅,需要抽象出一些模型出来,同时基于 C++面向对象编程的思想,需要考虑到一些类、继承相关。在 Golang 中,没有类、继承的概念,但是我们完全可以通过 struct 和 interface 来建立我们想要的任何模型。在我们的工程中,抽象出一种我自认为是类似 MVC 的模型,但是不完全一样,个人觉得这个模型抽象的比较好,容易扩展,模块清晰。对于使用 java 和 PHP 编程的同学对这个模型应该是再熟悉不过了,我这边通过代码来说明下这个模型
总结一下,model 对应 MVC 的 M,service 对应 MVC 的 C, 调用访问的地方对应 MVC 的 V
单例模式是一种常用的软件设计模式,在它的核心结构中只包含一个被称为单例的特殊类,通过单例模式可以保证系统中一个类有且仅有一个实例且该实例可以被外界访问。
在 Golang 中有一种非常优雅的姿势可以实现,就是通过 sync.Once 来实现,这个也是我在实际项目中所应用的,示例如下:
import "github.com/dropbox/godropbox/singleton"
var SingleService = singleton.NewSingleton(func() (interface{}, error) {
return &singleMsgProxy{
MsgModel: msg.MsgModelImpl,
}, nil
})singleton.NewSingleton 就是具体单例模式的实现,然后赋值给 SingleService,这样,在程序中任何需要获取这个对象的时候,就直接通过 SingleService 来调用,这个调用,系统会保证,里面的 singleMsgProxy 只会被初始化对象一次,这个 singleMsgProxy 就是 new 了一个对象,并且这个对象是只需要被初始化一次的。
Golang 工程 Layout 规范,网上有较多探讨,每个人的理解也会不一致,但是有些基础的理解是可以保持统一的:
一个简单示例如下:
$ tree -d -L 2
├── build
├── cmd
│ ├── apply
│ └── check
├── conf
├── config
├── docs
├── pkg
│ ├── apply
│ ├── check
│ ├── files
│ ├── k8s
│ └── options
└── vendor大家看 Kubernetes 的源码就可以发现,会有这么一个现象,Kubernetes 中会有很多二进制程序,然后每个程序,可能会有不同的指令,然后每个指令都会有很多命令行参数。如果大家对 Kubernetes 有一定了解,那么就知道 kubectl 会有如下命令:
kubectl apply -f 进行部署
kubectl delete -f 删除部署
kubectl get pod 获取 Pod那么 kubectl 这个二进制程序,如何能够优雅的支持不同的参数呢?
下面,还是以我实际项目工程中的应用为例,来进行演示。效果如下,程序 example 包含两个命令 apply 和 check,还有一个 help 命令:
$ ./example
Usage:
example[command]
Available Commands:
apply apply request by json file
check check request validity by json file
help Help about any command
Flags:
--config string config file[/.xx.yaml] (default "none")
-h, --help help for example
--mode string mode[cpu or all] (default "cpu")
Use "example[command] --help" for more information about a command.代码示例如下:
main 入口
package main
import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"os"
"example/cmd/apply"
"example/cmd/check"
"example/config"
)
func main() {
var cmdCheck = check.NewVPARequestCheck()
var cmdApply = apply.NewVPARequestApply()
var rootCmd = &cobra.Command{Use: "example"}
flags := rootCmd.PersistentFlags()
addFlags(flags)
rootCmd.AddCommand(cmdApply, cmdCheck)
if err := rootCmd.Execute(); err != nil {
panic(err)
}
}
func addFlags(flags *pflag.FlagSet) {
flags.StringVar(&config.Cfg.KubeConfig, "config", "none", "config file[/.xx.yaml]")
flags.StringVar(&config.Cfg.Mode, "mode", "cpu", "mode[cpu or all]")
}check 命令实现如下,具体 check 相关的 Run 方法忽略:
package check
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"example/config"
"example/pkg/check"
"example/pkg/files"
)
type RequestCheckOptions struct {
configPath string
}
func NewRequestCheckOptions() *RequestCheckOptions {
o := &RequestCheckOptions{}
return o
}
func NewVPARequestCheck() *cobra.Command {
o := NewRequestCheckOptions()
cmd := &cobra.Command{
Use: "check [json file]",
Short: "check request validity by json file",
Long: "check request by new request json file",
Args: cobra.MinimumNArgs(1),
RunE: func(c *cobra.Command, args []string) error {
if err := o.Run(args); err != nil {
return err
}
return nil
},
}
return cmd
}apply 命令如下,具体 apply 相关的 Run 方法忽略:
package apply
import (
"fmt"
"github.com/spf13/cobra"
"example/pkg/apply"
"example/pkg/files"
)
type RequestApplyOptions struct {
configPath string
}
func NewRequestApplyOptions() *RequestApplyOptions {
o := &RequestApplyOptions{}
return o
}
func NewVPARequestApply() *cobra.Command {
o := NewRequestApplyOptions()
cmd := &cobra.Command{
Use: "apply [json file]",
Short: "apply request by json file",
Long: "apply request by new request json file",
Args: cobra.MinimumNArgs(1),
RunE: func(c *cobra.Command, args []string) error {
if err := o.Run(args); err != nil {
return err
}
return nil
},
}
return cmd
}然后只需要在各自的 Run 方法中实现对应的逻辑即可。
腾讯程序员视频号最新视频