惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
博客园_首页
M
MIT News - Artificial intelligence
雷峰网
雷峰网
GbyAI
GbyAI
博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
S
SegmentFault 最新的问题
H
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 【当耐特】
V
Visual Studio Blog
月光博客
月光博客
G
Google Developers Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
云风的 BLOG
云风的 BLOG
美团技术团队
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans
有赞技术团队
有赞技术团队

博客园 - CHHC

从服务端到桌面端:Go + HTML 多端交付方案 跨平台桌面应用(内嵌 Web 界面 + Go RESTful API 服务) Dify 网页爬虫 Ai常用工具 Dify公司内部知识库权限隔离 智能体:OA行政助手 dify4: test-doc dify3: test-api dify2: test-image dify1: test-local-ollama 访问本地大模型 docker + dify + ollma 安装与配置 dify使用 eSIM SGP32 IPAd 适配移远EC200A OpenCPU 公钥解析 eSIM SGP32 自建符合GSMA规范的eIM平台(支持SGP32及SGP22卡接入) eSIM SGP32 eIM GSMA转码器(支持net及java) eSIM SGP32 证书 eSIM SGP32 生成eUICC所需的配置数据 eSIM SGP32 EuiccPackage包eimSignature和euiccSignEPR生成及校验 eSIM SGP32/SGP22 EUICC.SDK - IPAd RTSPShape-包含服务端及客户端 树莓派安装与配置 NetCore树莓派桌面应用程序 C#解析TLV数据(der -> asn1) golang 项目依赖备份 SGP32笔记 SoftSIM - swSIM eSIM SGP.22 LPA程序开发 - 协议解析 eSIM SGP.22 LPA程序开发 - 实现功能 PGP文件加解密
golang优化
CHHC · 2025-10-14 · via 博客园 - CHHC

工作池模式(提交10个任务给n个goroutine池处理)

package main

import (
    "fmt"
    "sync"
    "time"

    "github.com/panjf2000/ants"
)

// 模拟一个需要被处理的任务
func myTask(callName string, i int) {
    time.Sleep(100 * time.Millisecond) // 模拟任务处理耗时
    fmt.Printf("%s processing task %d\n", callName, i)
}

func main() {
    // 普通goroutine写法,创建10个goroutine处理任务
    for i := 0; i < 10; i++ {
        go func() {
            myTask("goroutine", i)
        }()
    }

    // 提交10个任务给n个goroutine池处理
    pool, err := ants.NewPool(5) // 创建固定大小的ants池(n个goroutine)
    if err != nil {
        fmt.Printf("Failed to create pool: %v\n", err)
        return
    }
    defer pool.Release()
    var wg sync.WaitGroup
    for i := 0; i < 10; i++ {
        wg.Add(1)
        task := func() {
            defer wg.Done()
            myTask("antsPool", i)
        }
        pool.Submit(task)
    }
    wg.Wait()
}

对象池模式(对象复用)

package main

import (
    "bytes"
    "fmt"
    "sync"
)

type Data struct {
    Value int
}

func createData() *Data {
    return &Data{Value: 42}
}

var dataPool = sync.Pool{
    New: func() any {
        return &Data{}
    },
}

var bufferPool = sync.Pool{
    New: func() any {
        return new(bytes.Buffer)
    },
}

func main() {
    for i := 0; i < 10; i++ {
        // 普通对象初始化
        obj1 := createData()
        fmt.Println(obj1.Value)

        // 使用对象池化 -- sync.Pool,使用场景:短期存活、可复用对象
        obj2 := dataPool.Get().(*Data)
        obj2.Value = 42
        fmt.Println(obj2.Value)
        dataPool.Put(obj2)
    }

    // 池化字节缓冲区
    buf := bufferPool.Get().(*bytes.Buffer)
    buf.Reset()
    buf.WriteString("Hello, pooled world!")
    fmt.Println(buf.String())
    bufferPool.Put(buf)
}