Go语言为何说它优雅?-- Golang中的几个常用初始化设计

package pool type Pool chan *Object // 初始化池,可指定容量
func New(total int) *Pool {
p := make(Pool, total) for i := ; i < total; i++ {
p <- new(Object)
}
return &p
}
p := pool.New()
select {
case obj := <-p:
obj.Do( /*希望池中对象进行的操作*/ )
p <- obj // 使用完毕归还到池中
default:
// 池资源无剩余,延迟重试或者直接告知失败等操作
return
}
package singleton type singleton map[string]string var (
once sync.Once instance singleton
) func New() singleton {
once.Do(func() {
instance = make(singleton)
}) return instance
}
s := singleton.New()
s["this"] = "that"
s2 := singleton.New()
fmt.Println("This is ", s2["this"])
//output: This is that
package data import "io" // 接口限定了能力(调用的方法)
type Store interface {
Open(string) (io.ReadWriteCloser, error)
} type StorageType int const (
DiskStorage StorageType = << iota
TempStorage
MemoryStorage
) func NewStore(t StorageType) Store {
// 通过枚举来创建实际创建的对象类型
switch t {
case MemoryStorage:
return newMemoryStorage( /*...*/ )
case DiskStorage:
return newDiskStorage( /*...*/ )
default:
return newTempStorage( /*...*/ )
}
}
s, _ := data.NewStore(data.MemoryStorage)
f, _ := s.Open("file") n, _ := f.Write([]byte("data"))
defer f.Close()
代码来源是Github上一个开源项目,项目内容是Go相关的常用设计模式的实现,很实用易懂: https://github.com/tmrts/go-patterns
对应的Gitbook地址:http://tmrts.com/go-patterns/structural/decorator.html

Go语言为何说它优雅?-- Golang中的几个常用初始化设计的更多相关文章
- golang中goroutine协程调度器设计策略
goroutine与线程 /* goroutine与线程1. 可增长的栈os线程一般都有固定的栈内存,通常为2MB,一个goroutine的在其声明周期开始时只有很小的栈(2KB),goroutine ...
- golang中for循环的常用用法
package main import "fmt" func main() { //printFormula() // 打印九九乘法表 //printLeftTriangle() ...
- iota: Golang 中优雅的常量
阅读约 11 分钟 注:该文作者是 Katrina Owen,原文地址是 iota: Elegant Constants in Golang 有些概念有名字,并且有时候我们关注这些名字,甚至(特别)是 ...
- Golang中设置函数默认参数的优雅实现
在Golang中,我们经常碰到要设置一个函数的默认值,或者说我定义了参数值,但是又不想传递值,这个在python或php一类的语言中很好实现,但Golang中好像这种方法又不行.今天在看Grpc源码时 ...
- 优雅处理Golang中的异常
我们在使用Golang时,不可避免会遇到异常情况的处理,与Java.Python等语言不同的是,Go中并没有try...catch...这样的语句块,我们知道在Java中使用try...catch.. ...
- 在Golang中使用C语言代码实例
转自:http://www.jb51.net/article/56720.htm cgo 使得在 Golang 中可以使用 C 代码. Hello World 为了有一个较为直观的了解,我们来看一个简 ...
- 延宕执行,妙用无穷,Go lang1.18入门精炼教程,由白丁入鸿儒,Golang中defer关键字延迟调用机制使用EP17
先行定义,延后执行.不得不佩服Go lang设计者天才的设计,事实上,defer关键字就相当于Python中的try{ ...}except{ ...}finally{...}结构设计中的finall ...
- golang中的race检测
golang中的race检测 由于golang中的go是非常方便的,加上函数又非常容易隐藏go. 所以很多时候,当我们写出一个程序的时候,我们并不知道这个程序在并发情况下会不会出现什么问题. 所以在本 ...
- 基础知识 - Golang 中的正则表达式
------------------------------------------------------------ Golang中的正则表达式 ------------------------- ...
随机推荐
- Confluence 6 访问日志脚本
日志访问脚本在连接:https://confluence.atlassian.com/download/attachments/133267635/Atlassian-accessLogScripts ...
- Confluence 6 配置校验和识别
校验你的设置 查看你 Confluence 当前使用的设置,请参考 Viewing System Properties 页面中的内容. 识别系统属性 请参考 Recognized System Pro ...
- Swift 设置某个对象的normal 属性找不到normal 解决方案
normal 等价于 UIControlState(rawValue: 0)
- select下拉框使用完毕后,重置按钮使其清空
需求描述:select下拉框后边有两个按钮,一个查询,一个重置,点击重置,select会清空之前选择的那个查询条件 解决思路:卧槽,这不so easy 么,用那个jQ封装的trigger函数搞定啊,对 ...
- Python字符串编码转换
使用encode()方法编码 str.encode([encoding="utf-8"][,errors="strict"]) str:表示需要转换的字符串 e ...
- ajax-简单参数方法实现阴影效果
注: 简单参数 (按照参数的数量和位置传递参数) 使用时按照位置.数量传递 shadow.js函数 //简单参数实现方式/** slices:阴影* opacity:透明度* zIndex:层级* * ...
- AR 前言
LBS 基于位置的服务,是指通过电信移动运营商的无线电通讯网络或外部定位方式,获取移动终端用户的位置信息,在GIS平台的支持下,为用户提供相应服务的一种增值业务. 它包括两层含义:首先是确定移动设备或 ...
- mysql 5.7 安装
linux(CentOS6.7) 环境Mysql 5.7.17安装教程分享给大家,供大家参考,具体内容如下: 1系统约定 安装文件下载目录:/data/software Mysql目录安装位置:/us ...
- GZipStream 压缩与解压数据
简介:此类表示 GZip 数据格式,它使用无损压缩和解压缩文件的行业标准算法.这种格式包括一个检测数据损坏的循环冗余校验值.GZip 数据格式使用的算法与 DeflateStream 类的算法相同,但 ...
- NetCore 生成RSA公私钥对,公钥加密私钥解密,私钥加密公钥解密
using Newtonsoft.Json; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Encodings; using ...