Go 中几种常见的编程模式

模式可以理解成最佳实践,或者是约定俗成的规范或套路,熟悉一些常见模式可以方便理解项目代码。本文是参考左耳朵耗子的专栏文章做的笔记,另外也缅怀一下耗子叔。

slice 切片的数据共享与扩容迁移

切片的数据共享

切片就像是在数组上开的窗口,透过切片窗口可以看到和修改底层数组。

这段代码中,foo 和 bar 都是来源于一个底层数组的切片,在 foo 或 bar 任意一个切片做修改,都会影响到另一个切片,或者说在另一个切片也能看到其他切片做出的修改。

foo = make([]int5)
foo[3] = 42
foo[4] = 100

bar  := foo[1:4]
bar[1] = 99

左图是未修改时的示例,右图是修改后的示例。

alt

append 覆盖后续切片现象

给一个切片 append 元素时,如果 append 后超出原有切片的容量则会发生扩容,确切来说应该是重分配内存把原有元素迁移到新地址,而如果 append 后没超过原有容量则不发生扩容迁移。

下面代码是一个原空间不足,而发生扩容迁移例子

a := make([]int32)
b := a[1:16]
a = append(a, 1)
a[2] = 42
alt

往 a 中 append 元素,a 原本已经是满格 32 容量了,append 之后发生扩容迁移,与 b 不再共享数据,之后在 a 上修改反映不到 b 上。

下面代码表示的是 append 后容量足够,在原位修改且因为数据共享导致覆盖掉了后面的切片

func main() {
    path := []byte("AAAA/BBBBBBBBB")
    sepIndex := bytes.IndexByte(path,'/')

    dir1 := path[:sepIndex]
    dir2 := path[sepIndex+1:]

    fmt.Println("dir1 =>",string(dir1)) //prints: dir1 => AAAA
    fmt.Println("dir2 =>",string(dir2)) //prints: dir2 => BBBBBBBBB

    dir1 = append(dir1,"suffix"...)

    fmt.Println("dir1 =>",string(dir1)) //prints: dir1 => AAAAsuffix
    fmt.Println("dir2 =>",string(dir2)) //prints: dir2 => uffixBBBB
}
alt

要避免这个问题发生可以在生成切片 dir1 时,指定其容量就等于初始化长度,使得一旦后续再往里 append 元素必然导致扩容迁移。 dir1 := path[:sepIndex:sepIndex]

使用 reflect.DeepEqual() 深度比较

深度比较可以深入复杂类型内部逐个属性比较

import (
    "fmt"
    "reflect"
)

func main() {

    v1 := data{}
    v2 := data{}
    fmt.Println("v1 == v2:",reflect.DeepEqual(v1,v2))
    //prints: v1 == v2: true

    m1 := map[string]string{"one""a","two""b"}
    m2 := map[string]string{"two""b""one""a"}
    fmt.Println("m1 == m2:",reflect.DeepEqual(m1, m2))
    //prints: m1 == m2: true

    s1 := []int{123}
    s2 := []int{123}
    fmt.Println("s1 == s2:",reflect.DeepEqual(s1, s2))
    //prints: s1 == s2: true
}

Functional Options 高阶函数实现可选配置

初始化某实例时,有多种可选的初始化参数,可以使用单独 Config 对象,也可以使用 Builder 模式。这里介绍一种通过函数式编程来初始化的做法。

Server 就是要实例化的对象


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

首先定义一种函数类型,接收 Server 的指针,目的是修改 Server 内部的属性

type Option func(*Server)

定义一组返回 Option 的方法,接收定制参数,将参数写入到 Option 中,实现高阶函数的效果。


// 返回一个 Option,Option 内部来修改 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
 }
}

最后定义初始化函数,函数除了必选参数外,其他可选参数通过变长参数传入,通过 for 循环启动每个 option 方法设置 server 实例,实现可选参数初始化。

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

func FunctionOptions() {
 s1, _ := NewServer("localhost"1024)
 s2, _ := NewServer("localhost"2048, Protocol("udp"), TLS(&tls.Config{}))
 s3, _ := NewServer("0.0.0.0"8080, Timeout(300*time.Second), MaxConns(1000))

 fmt.Println(s1)
 fmt.Println(s2)
 fmt.Println(s3)
}
&{localhost 1024 tcp 30s 1000 <nil>}
&{localhost 2048 udp 30s 1000 0x1400011a000}
&{0.0.0.0 8080 tcp 5m0s 1000 <nil>}

实际上就是利用了高阶函数来初始化变量,间接实现保存状态的效果。

通过嵌入组合的方式实现控制反转 IoC

在业务逻辑代码中耦合进控制逻辑,会导致在编写业务逻辑时需要处理业务之外的事,而且控制逻辑耦合进业务中,只能适用于当前业务逻辑,无法被复用。

控制反转是一种解耦思想,将原本耦合在业务逻辑中的控制逻辑单独拆出来实现,不再让业务逻辑在处理业务的同时还要去实现控制逻辑,而是专注处理业务。

假设现在有一个 IntSet 结构,用来处理整型数据:

type IntSet struct {
 data map[int]bool
}

func NewIntSet() IntSet {
 return IntSet{make(map[int]bool)}
}
func (set *IntSet) Add(x int) {
 set.data[x] = true
}
func (set *IntSet) Delete(x int) {
 delete(set.data, x)
}
func (set *IntSet) Contains(x int) bool {
 return set.data[x]
}

现在要为其增加 undo 功能

一种做法是在 IntSet 基础上完整实现一遍 undo 功能,即 IntSet 自己维护一个 do functions 切片,自己维护入栈出栈操作。

type UndoableIntSet struct { // Poor style
 IntSet    // Embedding (delegation)
 functions []func()
}

func NewUndoableIntSet() UndoableIntSet {
 return UndoableIntSet{NewIntSet(), nil}
}

func (set *UndoableIntSet) Add(x int) { // Override
 if !set.Contains(x) {
  set.data[x] = true
  set.functions = append(set.functions, func() { set.Delete(x) })
 } else {
  set.functions = append(set.functions, nil)
 }
}

func (set *UndoableIntSet) Delete(x int) { // Override
 if set.Contains(x) {
  delete(set.data, x)
  set.functions = append(set.functions, func() { set.Add(x) })
 } else {
  set.functions = append(set.functions, nil)
 }
}

func (set *UndoableIntSet) Undo() error {
 if len(set.functions) == 0 {
  return errors.New("No functions to undo")
 }
 index := len(set.functions) - 1
 if function := set.functions[index]; function != nil {
  function()
  set.functions[index] = nil // For garbage collection
 }
 set.functions = set.functions[:index]
 return nil
}

上面代码这就是所谓的控制逻辑依赖业务逻辑,或者说控制逻辑被耦合进了业务逻辑之中。undo 是控制逻辑,IntSet 是业务逻辑,IntSet 在处理原本整数数据相关的事,现在却要关心函数调用次序的事,而 undo 控制逻辑嵌入到了 IntSet 中,只能服务于 IntSet,而 undo 原本是一个通用逻辑,可能别的数据结构也会用到 undo 功能。

能否将控制逻辑抽出来,单独实现,然后大家共用一套协议,面对协议开发就好了?

现声明一个 Undo 类型并给 Undo 类型声明几个方法,专门处理 undo 操作,它实际上就是一组 function 的切片


type Undo []func()

func (undo *Undo) Add(function func()) {
 *undo = append(*undo, function)
}

func (undo *Undo) Undo() error {
 functions := *undo
 if len(functions) == 0 {
  return errors.New("No functions to undo")
 }
 index := len(functions) - 1
 if function := functions[index]; function != nil {
  function()
  functions[index] = nil // For garbage collection
 }
 *undo = functions[:index]
 return nil
}

IntSet 通过嵌入一个 Undo 结构,间接实现 undo 功能,这样控制逻辑封装在 Undo 中,业务逻辑 IntSet 只关心处理整型数据就好了。


type IntSet struct {
 data map[int]bool
 undo Undo
}

func NewIntSet() IntSet {
 return IntSet{data: make(map[int]bool)}
}

func (set *IntSet) Undo() error {
 return set.undo.Undo()
}

func (set *IntSet) Contains(x int) bool {
 return set.data[x]
}

func (set *IntSet) Add(x int) {
 if !set.Contains(x) {
  set.data[x] = true
  set.undo.Add(func() { set.Delete(x) })
 } else {
  set.undo.Add(nil)
 }
}

func (set *IntSet) Delete(x int) {
 if set.Contains(x) {
  delete(set.data, x)
  set.undo.Add(func() { set.Add(x) })
 } else {
  set.undo.Add(nil)
 }
}

本质还是面向接口编程和组合优于继承的思想,一方面是制定交互协议,另一方面是根据自身需求选择合适组件组合到一起。

通过反射实现泛型版的 Map Reduce Filter

泛型函数就是接收任意类型的 slice,处理后返回相应类型的结果。

最直接的不做类型判断的泛型 Map

func Map(data interface{}, fn interface{}) []interface{} {
    vfn := reflect.ValueOf(fn)
    vdata := reflect.ValueOf(data)
    result := make([]interface{}, vdata.Len())

    for i := 0; i < vdata.Len(); i++ {
        result[i] = vfn.Call([]reflect.Value{vdata.Index(i)})[0].Interface()
    }
    return result
}

加入类型判断的 Map

func Transform(slice, function interface{}) interface{} {
  return transform(slice, function, false)
}

func TransformInPlace(slice, function interface{}) interface{} {
  return transform(slice, function, true)
}

func transform(slice, function interface{}, inPlace bool) interface{} {
 
  //check the `slice` type is Slice
  sliceInType := reflect.ValueOf(slice)
  if sliceInType.Kind() != reflect.Slice {
    panic("transform: not slice")
  }

  //check the function signature
  fn := reflect.ValueOf(function)
  elemType := sliceInType.Type().Elem()
  if !verifyFuncSignature(fn, elemType, nil) {
    panic("trasform: function must be of type func(" + sliceInType.Type().Elem().String() + ") outputElemType")
  }

  sliceOutType := sliceInType
  if !inPlace {
    sliceOutType = reflect.MakeSlice(reflect.SliceOf(fn.Type().Out(0)), sliceInType.Len(), sliceInType.Len())
  }
  for i := 0; i < sliceInType.Len(); i++ {
    sliceOutType.Index(i).Set(fn.Call([]reflect.Value{sliceInType.Index(i)})[0])
  }
  return sliceOutType.Interface()

}

func verifyFuncSignature(fn reflect.Value, types ...reflect.Type) bool {

  //Check it is a funciton
  if fn.Kind() != reflect.Func {
    return false
  }
  // NumIn() - returns a function type's input parameter count.
  // NumOut() - returns a function type's output parameter count.
  if (fn.Type().NumIn() != len(types)-1) || (fn.Type().NumOut() != 1) {
    return false
  }
  // In() - returns the type of a function type's i'th input parameter.
  for i := 0; i < len(types)-1; i++ {
    if fn.Type().In(i) != types[i] {
      return false
    }
  }
  // Out() - returns the type of a function type's i'th output parameter.
  outType := types[len(types)-1]
  if outType != nil && fn.Type().Out(0) != outType {
    return false
  }
  return true
}

加入类型判断的 Reduce

func Reduce(slice, pairFunc, zero interface{}) interface{} {
  sliceInType := reflect.ValueOf(slice)
  if sliceInType.Kind() != reflect.Slice {
    panic("reduce: wrong type, not slice")
  }

  len := sliceInType.Len()
  if len == 0 {
    return zero
  } else if len == 1 {
    return sliceInType.Index(0)
  }

  elemType := sliceInType.Type().Elem()
  fn := reflect.ValueOf(pairFunc)
  if !verifyFuncSignature(fn, elemType, elemType, elemType) {
    t := elemType.String()
    panic("reduce: function must be of type func(" + t + ", " + t + ") " + t)
  }

  var ins [2]reflect.Value
  ins[0] = sliceInType.Index(0)
  ins[1] = sliceInType.Index(1)
  out := fn.Call(ins[:])[0]

  for i := 2; i < len; i++ {
    ins[0] = out
    ins[1] = sliceInType.Index(i)
    out = fn.Call(ins[:])[0]
  }
  return out.Interface()
}

加入类型判断的 Filter

func Filter(slice, function interface{}) interface{} {
  result, _ := filter(slice, function, false)
  return result
}

func FilterInPlace(slicePtr, function interface{}) {
  in := reflect.ValueOf(slicePtr)
  if in.Kind() != reflect.Ptr {
    panic("FilterInPlace: wrong type, " +
      "not a pointer to slice")
  }
  _, n := filter(in.Elem().Interface(), function, true)
  in.Elem().SetLen(n)
}

var boolType = reflect.ValueOf(true).Type()

func filter(slice, function interface{}, inPlace bool) (interface{}, int) {

  sliceInType := reflect.ValueOf(slice)
  if sliceInType.Kind() != reflect.Slice {
    panic("filter: wrong type, not a slice")
  }

  fn := reflect.ValueOf(function)
  elemType := sliceInType.Type().Elem()
  if !verifyFuncSignature(fn, elemType, boolType) {
    panic("filter: function must be of type func(" + elemType.String() + ") bool")
  }

  var which []int
  for i := 0; i < sliceInType.Len(); i++ {
    if fn.Call([]reflect.Value{sliceInType.Index(i)})[0].Bool() {
      which = append(which, i)
    }
  }

  out := sliceInType

  if !inPlace {
    out = reflect.MakeSlice(sliceInType.Type(), len(which), len(which))
  }
  for i := range which {
    out.Index(i).Set(sliceInType.Index(which[i]))
  }

  return out.Interface(), len(which)
}

总的来说就是验证 slice 类型,验证 function 类型和参数数量,验证 slice 元素类型与函数参数类型等等一切你能想到的正常代码中会报错的地方。

通过代码生成的方式生成出泛型代码

go genarate 命令可以识别代码中的特定格式的注释,执行命令并传递参数,可以利用这个特点预先编写命令生成代码,避免在运行时使用反射或断言增加性能消耗,还可以保持代码的简洁易读。

写一个生成通用容器的模板

package PACKAGE_NAME
type GENERIC_NAMEContainer struct {
    s []GENERIC_TYPE
}
func NewGENERIC_NAMEContainer() *GENERIC_NAMEContainer {
    return &GENERIC_NAMEContainer{s: []GENERIC_TYPE{}}
}
func (c *GENERIC_NAMEContainer) Put(val GENERIC_TYPE) {
    c.s = append(c.s, val)
}
func (c *GENERIC_NAMEContainer) Get() GENERIC_TYPE {
    r := c.s[0]
    c.s = c.s[1:]
    return r
}

针对模版做替换的脚本

#!/bin/bash

set -e

SRC_FILE=${1}
PACKAGE=${2}
TYPE=${3}
DES=${4}
#uppcase the first char
PREFIX="$(tr '[:lower:]' '[:upper:]' <<< ${TYPE:0:1})${TYPE:1}"

DES_FILE=$(echo ${TYPE}| tr '[:upper:]' '[:lower:]')_${DES}.go

sed 's/PACKAGE_NAME/'"${PACKAGE}"'/g' ${SRC_FILE} | \
    sed 's/GENERIC_TYPE/'"${TYPE}"'/g' | \
    sed 's/GENERIC_NAME/'"${PREFIX}"'/g' > ${DES_FILE}

在实际代码中添加注释,注释格式为 go:generate command args,然后执行 go generate,会扫描 go 代码中的注释并执行。

//go:generate ./gen.sh ./template/container.tmp.go gen uint32 container
func generateUint32Example() {
    var u uint32 = 42
    c := NewUint32Container()
    c.Put(u)
    v := c.Get()
    fmt.Printf("generateExample: %d (%T)\n", v, v)
}

//go:generate ./gen.sh ./template/container.tmp.go gen string container
func generateStringExample() {
    var s string = "Hello"
    c := NewStringContainer()
    c.Put(s)
    v := c.Get()
    fmt.Printf("generateExample: %s (%T)\n", v, v)
}

最后生成两个容器代码

package gen

type Uint32Container struct {
    s []uint32
}
func NewUint32Container() *Uint32Container {
    return &Uint32Container{s: []uint32{}}
}
func (c *Uint32Container) Put(val uint32) {
    c.s = append(c.s, val)
}
func (c *Uint32Container) Get() uint32 {
    r := c.s[0]
    c.s = c.s[1:]
    return r
}
package gen

type StringContainer struct {
    s []string
}
func NewStringContainer() *StringContainer {
    return &StringContainer{s: []string{}}
}
func (c *StringContainer) Put(val string) {
    c.s = append(c.s, val)
}
func (c *StringContainer) Get() string {
    r := c.s[0]
    c.s = c.s[1:]
    return r
}

高阶函数之装饰器

func WithServerHeader(h http.HandlerFunc) http.HandlerFunc {
 return func(w http.ResponseWriter, r *http.Request) {
  log.Println("--->WithServerHeader()")
  w.Header().Set("Server""HelloServer v0.0.1")
  h(w, r)
 }
}

func WithAuthCookie(h http.HandlerFunc) http.HandlerFunc {
 return func(w http.ResponseWriter, r *http.Request) {
  log.Println("--->WithAuthCookie()")
  cookie := &http.Cookie{Name: "Auth", Value: "Pass", Path: "/"}
  http.SetCookie(w, cookie)
  h(w, r)
 }
}

func WithBasicAuth(h http.HandlerFunc) http.HandlerFunc {
 return func(w http.ResponseWriter, r *http.Request) {
  log.Println("--->WithBasicAuth()")
  cookie, err := r.Cookie("Auth")
  if err != nil || cookie.Value != "Pass" {
   w.WriteHeader(http.StatusForbidden)
   return
  }
  h(w, r)
 }
}

func WithDebugLog(h http.HandlerFunc) http.HandlerFunc {
 return func(w http.ResponseWriter, r *http.Request) {
  log.Println("--->WithDebugLog")
  r.ParseForm()
  log.Println(r.Form)
  log.Println("path", r.URL.Path)
  log.Println("scheme", r.URL.Scheme)
  log.Println(r.Form["url_long"])
  for k, v := range r.Form {
   log.Println("key:", k)
   log.Println("val:", strings.Join(v, ""))
  }
  h(w, r)
 }
}
func hello(w http.ResponseWriter, r *http.Request) {
 log.Printf("Recieved Request %s from %s\n", r.URL.Path, r.RemoteAddr)
 fmt.Fprintf(w, "Hello, World! "+r.URL.Path)
}

func main() {
 http.HandleFunc("/v1/hello", WithServerHeader(WithAuthCookie(hello)))
 http.HandleFunc("/v2/hello", WithServerHeader(WithBasicAuth(hello)))
 http.HandleFunc("/v3/hello", WithServerHeader(WithBasicAuth(WithDebugLog(hello))))
 err := http.ListenAndServe(":8080"nil)
 if err != nil {
  log.Fatal("ListenAndServe: ", err)
 }
}

利用 channel 实现 pipeline 模式

func echo(nums []int) <-chan int {
  out := make(chan int)
  go func() {
    for _, n := range nums {
      out <- n
    }
    close(out)
  }()
  return out
}

func sq(in <-chan int) <-chan int {
  out := make(chan int)
  go func() {
    for n := range in {
      out <- n * n
    }
    close(out)
  }()
  return out
}

func odd(in <-chan int) <-chan int {
  out := make(chan int)
  go func() {
    for n := range in {
      if n%2 != 0 {
        out <- n
      }
    }
    close(out)
  }()
  return out
}

func sum(in <-chan int) <-chan int {
  out := make(chan int)
  go func() {
    var sum = 0
    for n := range in {
      sum += n
    }
    out <- sum
    close(out)
  }()
  return out
}

var nums = []int{12345678910}
for n := range sum(sq(odd(echo(nums)))) {
  fmt.Println(n)
}

或者单独一个 pipeline 函数,看着更合适

本文由 mdnice 多平台发布

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值