2026年6月7日 星期日

AI News Digest - 2026-06-07

AI News Digest - 2026-06-07

 ai     anthropic    google    github  

Summary

The AI industry has decisively shifted from simple chatbots to autonomous agentic systems. Key players like Google and GitHub are launching platforms that allow AI agents to handle entire development lifecycles, from coding to CI/CD management. However, this increased autonomy brings new challenges in security and cost management. Companies like Uber are already implementing strict monthly budget caps for AI tools as token consumption skyrockets. Meanwhile, developers are turning to technologies like WASM-based sandboxing to safely execute AI-generated code. New model releases are prioritizing reasoning consistency and agentic reliability over sheer size.

AI 產業已果斷從簡單的聊天機器人轉向自主代理系統(Agentic Systems)。Google 和 GitHub 等主要參與者正在推出讓 AI 代理能夠處理整個開發生命週期的平台,從編碼到 CI/CD 管理。然而,這種增加的自主性帶來了安全和成本管理方面的新挑戰。隨著 Token 消耗量激增,Uber 等公司已經開始對 AI 工具實施嚴格的每月預算上限。同時,開發者正在轉向基於 WASM 的沙盒技術,以安全地執行 AI 生成的代碼。新發布的模型正優先考慮推理一致性和代理可靠性,而非單純的模型規模。


Anthropic

Introducing Claude Opus 4.8

  • Summary: A major upgrade to the Opus class, specifically optimized for coding, agentic tasks, and professional workflows requiring high consistency over long contexts.
  • 摘要:Opus 系列的重大升級,特別針對編碼、代理任務和需要長上下文高度一致性的專業工作流進行了優化。
  • Why it matters: Developers using Claude for complex refactoring or multi-file agentic workflows will see fewer "hallucinations" in logic and better adherence to architectural constraints.
  • 為什麼重要:使用 Claude 進行複雜重構或多文件代理工作流的開發者將看到更少的邏輯「幻覺」,並能更好地遵循架構約束。
  • Key takeaway: Enhanced "agentic consistency"—the model is better at maintaining state and following multi-step instructions without drifting.
  • 關鍵點:增強了「代理一致性」——模型在保持狀態和遵循多步指令方面表現更好,不會產生偏離。

Anthropic Confidentially Submits Draft S-1

  • Summary: Anthropic has officially begun the process for an IPO following a massive $65B Series H funding round.
  • 摘要:Anthropic 在獲得 650 億美元的巨額 H 輪融資後,已正式開始 IPO 進程。
  • Why it matters: Signals the long-term stability of the Claude ecosystem. Anthropic is positioning itself as the primary enterprise-grade alternative to OpenAI.
  • 為什麼重要:標誌著 Claude 生態系統的長期穩定性。Anthropic 正將自己定位為 OpenAI 的主要企業級替代方案。
  • Key takeaway: Anthropic is preparing for public markets, solidifying its role as a major AI infrastructure provider.
  • 關鍵點:Anthropic 正在為進入公開市場做準備,鞏固其作為主要 AI 基礎設施提供商的角色。

Google DeepMind

Introducing Google Antigravity 2.0

  • Summary: A comprehensive "agentic development platform" that integrates Gemini 3.5 directly into the developer's environment with native support for subagents, hooks, and scheduled tasks.
  • 摘要:一個全面的「代理開發平台」,將 Gemini 3.5 直接集成到開發者環境中,並原生支持子代理、鉤子(hooks)和計劃任務。
  • Why it matters: It moves beyond a simple IDE plugin to a programmable orchestration layer where developers can build their own custom agents to manage CI/CD, documentation, and testing.
  • 為什麼重要:它超越了簡單的 IDE 插件,轉向一個可編程的編排層,開發者可以在其中構建自己的自定義代理來管理 CI/CD、文檔和測試。
  • Key takeaway: Subagents & Hooks—the ability to dispatch specialized agents for specific sub-tasks within a larger workflow.
  • 關鍵點:子代理與鉤子——在更大的工作流中為特定子任務(例如「安全審計代理」)派遣專門代理的能力。

Gemini 3.5: Frontier Intelligence with Action

  • Summary: The latest iteration of Gemini, focusing on "action-oriented" intelligence—the ability to use tools and interact with external systems with higher reliability.
  • 摘要:Gemini 的最新迭代,專注於「行動導向」的智能——以更高的可靠性使用工具並與外部系統交互的能力。
  • Why it matters: Lower latency and better tool-calling accuracy make it ideal for real-time CLI tools and automated system administration.
  • 為什麼重要:更低的延遲和更好的工具調用準確性使其成為實時 CLI 工具和自動化系統管理的理想選擇。
  • Key takeaway: Improved reliability in interacting with real-world APIs and systems.
  • 關鍵點:在與現實世界的 API 和系統交互方面提高了可靠性。

Simon Willison

Running Python code in a sandbox with MicroPython and WASM

  • Summary: Willison released micropython-wasm, a package that allows running untrusted Python code in a secure, deterministic sandbox using WebAssembly.
  • 摘要:Willison 發布了 micropython-wasm,這是一個允許使用 WebAssembly 在安全、確定的沙盒中運行不可信 Python 代碼的軟體包。
  • Practical insight: Essential for developers building "Agentic" systems that need to execute AI-generated code locally without risking the host machine's security.
  • 實際見解:對於構建「代理」系統的開發者來說至關重要,這些系統需要在本地執行 AI 生成的代碼,而不冒主機安全風險。
  • Useful tools or techniques mentioned: Provides a "Lethal Trifecta" defense—cutting off the exfiltration vector by ensuring the sandbox has no network access.
  • 提到的實用工具或技術:提供「致命三連」防禦——通過確保沙盒沒有網絡訪問權限來切斷數據外洩途徑。

Uber Caps AI Tool Spending at $1,500/Month

  • Summary: Uber implemented a strict monthly spending limit per AI coding tool after blowing through its 2026 AI budget in just four months.
  • 摘要:Uber 在短短四個月內耗盡了 2026 年的 AI 預算後,對每個 AI 編碼工具實施了嚴格的每月支出限制。
  • Practical insight: Developers should expect "token quotas" to become a standard part of the corporate engineering environment. Efficiency in prompting is now a cost-saving skill.
  • 實際見解:開發者應預期「Token 配額」將成為企業工程環境的標準組成部分。提示詞效率現在是一項節省成本的技能。
  • Useful tools or techniques mentioned: Token discipline and engineering management of AI costs.
  • 提到的實用工具或技術:Token 紀律和 AI 成本的工程管理。

Google Antigravity

Antigravity CLI & SDK

  • Summary: Release of the official CLI and SDK for the Antigravity platform, allowing developers to script agentic workflows.
  • 摘要:發布了 Antigravity 平台的官方 CLI 和 SDK,允許開發者編寫代理工作流腳本。
  • Interesting innovation: Enables "Headless AI"—running agents as part of a headless build process or cron job rather than just inside an IDE.
  • 有趣的創新:實現了「無頭 AI」——將代理作為無頭構建過程或定時任務的一部分運行,而不僅僅是在 IDE 內部。
  • Real-world impact: Developers can now automate large-scale repository management using specialized agent scripts.
  • 現實世界影響:開發者現在可以使用專門的代理腳本自動化大規模存儲庫管理。
  • Key takeaway: /fleet command—allows the CLI to dispatch multiple agents in parallel across a codebase to solve distributed problems.
  • 關鍵點:/fleet 指令——允許 CLI 在代碼庫中並行派遣多個代理來解決分佈式問題。

GitHub Copilot

Improving Token Efficiency in GitHub Agentic Workflows

  • Summary: A technical deep dive into how GitHub reduced API costs for their internal agents by instrumenting workflows and identifying "token-heavy" inefficiencies.
  • 摘要:深入探討 GitHub 如何通過儀表化工作流和識別「Token 密集型」效率低下問題,來降低其內部代理的 API 成本。
  • Developer productivity impact: Teams can learn how to build their own observability layers for AI-driven development.
  • 開發者生產力影響:團隊可以學習如何為 AI 驅動的開發構建自己的觀察層。
  • Important feature or workflow: Dominatory Analysis—a method for validating agentic behavior when the "correct" output isn't deterministic.
  • 重要功能或工作流:支配性分析(Dominatory Analysis)——一種在「正確」輸出不確定時驗證代理行為的方法。

Take your local GitHub sessions anywhere

  • Summary: Generally available "Remote Control" for Copilot sessions, allowing developers to start work in VS Code and finish/review it via GitHub Mobile.
  • 摘要:Copilot 會話的「遠程控制」功能已正式發布,允許開發者在 VS Code 中開始工作,並通過 GitHub Mobile 完成或審查。
  • Developer productivity impact: Increases the fluidity of the "AI-assisted" workflow, allowing for quick reviews and minor fixes on the go.
  • 開發者生產力影響:增加了「AI 輔助」工作流的流動性,允許隨時隨地進行快速審查和微小修復。
  • Important feature or workflow: Seamless transition between desktop and mobile for code reviews and minor adjustments.
  • 重要功能或工作流:桌面和移動端之間在代碼審查和微調方面的無縫轉換。

Overall Trends

  • The Rise of Agentic Engineering: A shift from chat interfaces to autonomous agents managing repos and CI/CD.
  • 代理工程的興起: 從聊天界面轉向管理存儲庫和 CI/CD 的自主代理。
  • Token Economics & Cost Control: Corporate budgeting for AI is becoming a major constraint, leading to "token discipline."
  • Token 經濟與成本控制: 企業的 AI 預算正成為主要約束,導致了「Token 紀律」。
  • Deterministic Security for AI: Using WASM and sandboxing to safely execute non-deterministic AI code.
  • AI 的確定性安全: 使用 WASM 和沙盒技術安全地執行非確定性的 AI 代碼。
  • Specialized Reasoning Models: Smaller but more consistent models optimized for multi-step agentic tasks.
  • 專門的推理模型: 針對多步代理任務進行了優化,規模較小但更具一致性的模型。

2026年5月30日 星期六

[AI] 2025-05-30 News

AI News Digest - 2026-05-30

 ai     anthropic    google    github  

Summary

The AI landscape in late May 2026 is dominated by massive financial moves and significant model upgrades. Anthropic secured a monumental $65B funding round, signaling continued high-stakes investment in frontier models. Simultaneously, they released Claude Opus 4.8, focusing on professional performance and consistency. Google DeepMind introduced Gemini Omni and Gemini 3.5, emphasizing multimodal versatility and agentic action. GitHub continues to refine its ecosystem with session syncing, while independent developers like Simon Willison track the steady improvements in tools like Datasette.

2026年5月底的AI領域主要受大規模融資和重大模型升級所主導。Anthropic獲得了破紀錄的650億美元融資,顯示了對前沿模型的持續高額投資。同時,他們發布了 Claude Opus 4.8,專注於專業性能和穩定性。Google DeepMind 推出了 Gemini Omni 和 Gemini 3.5,強調多模態通用性和代理行動能力。GitHub 持續優化其生態系統,而像 Simon Willison 這樣的獨立開發者則關注 Datasette 等工具的穩定進步。


Anthropic

Introducing Claude Opus 4.8

  • Summary: An upgrade to the Opus class of models, featuring stronger performance across coding, agentic tasks, and professional work, with improved consistency for long-running tasks.
    Opus 系列模型的升級版本,在程式碼編寫、代理任務和專業工作方面表現更強,並提高了長時間運行任務的一致性。
  • Why it matters: It bridges the gap between raw intelligence and reliable automation, making it more suitable for complex enterprise workflows.
    它縮小了原始智能與可靠自動化之間的差距,使其更適合複雜的企業工作流程。
  • Key takeaway: Professional AI is shifting from "one-off answers" to "reliable session-long agents."
    專業 AI 正在從「一次性回答」轉向「可靠的會話級代理」。

Anthropic raises $65B in Series H funding

  • Summary: A massive new funding round to support continued research and development of frontier AI models at a nearly $1T valuation.
    一輪巨額的新融資,以近 1 兆美元的估值支持前沿 AI 模型的持續研發。
  • Why it matters: This confirms that the compute and talent requirements for frontier models are still scaling exponentially.
    這證實了前沿模型對算力和人才的需求仍在呈指數級增長。
  • Key takeaway: The capital moat for top-tier AI providers is becoming insurmountable for smaller players.
    頂級 AI 供應商的資本護城河正變得讓小型競爭者難以逾越。

Google DeepMind

Introducing Gemini Omni

  • Summary: A new multimodal model designed to create "anything from anything," expanding the generative capabilities across all media types simultaneously.
    一款全新的多模態模型,旨在實現「任何形式到任何形式」的創作,同時擴展了所有媒體類型的生成能力。
  • Why it matters: It moves away from siloed modalities (text, image, audio) toward a truly unified latent space for all digital information.
    它告別了孤立的多模態(文本、圖像、音頻),轉向一個真正統一的所有數位資訊潛在空間。
  • Key takeaway: Multi-modal isn't a feature anymore; it's the fundamental architecture.
    多模態不再只是一項功能;它是基礎架構。

Gemini 3.5: Frontier Intelligence with Action

  • Summary: The latest iteration of Gemini focusing on "actionable intelligence" and autonomous execution of multi-step processes.
    Gemini 的最新迭代,專注於「可執行的智能」和多步驟流程的自主執行。
  • Why it matters: Google is doubling down on the "agent" paradigm to integrate AI more deeply into its Workspace and Cloud ecosystems.
    Google 正在加倍投入「代理」範式,以將 AI 更深地整合到其 Workspace 和雲端生態系統中。
  • Key takeaway: The focus of LLMs has officially shifted from "knowing" to "doing."
    LLM 的焦點已正式從「知識」轉向「行動」。

Simon Willison

Release datasette 1.0a31

  • Summary: A significant alpha release for Datasette, introducing the ability to execute write queries and save stored queries directly.
    Datasette 的一個重要 Alpha 版本,引入了直接執行寫入查詢和儲存已存查詢的功能。
  • Practical insight: This makes Datasette a more interactive tool for data management rather than just a read-only viewer.
    這使得 Datasette 成為一個更具互動性的數據管理工具,而不僅僅是一個唯讀的檢視器。
  • Useful tools: Datasette continues to be a core recommendation for developers working with SQLite and LLM-generated data.
    對於處理 SQLite 和 LLM 生成數據的開發者來說,Datasette 仍然是核心推薦工具。

GitHub Copilot

Take your local GitHub sessions anywhere

  • Summary: Remote control for GitHub Copilot sessions is now generally available, allowing developers to sync work state between IDEs, CLI, and mobile.
    GitHub Copilot 會話的遠端控制現已正式發布,允許開發者在 IDE、CLI 和行動裝置之間同步工作狀態。
  • Developer productivity impact: Reduces friction when switching devices or environments, maintaining the "context" of a complex coding task.
    減少切換設備或環境時的摩擦,維持複雜編碼任務的「上下文」。
  • Important feature: Cross-platform context syncing is a major win for developer flow.
    跨平台上下文同步是開發者流程的一大勝利。

Overall Trends

  • Massive Consolidation of Capital: Anthropic's valuation and funding levels suggest the cost of staying at the "frontier" remains astronomical.
    巨額資本整合: Anthropic 的估值和融資水平表明,保持在「前沿」的成本仍然極高。
  • Agentic Shift: Both Gemini 3.5 and Claude Opus 4.8 are pivoting heavily toward "agentic" capabilities—models that don't just talk but act.
    代理化轉向: Gemini 3.5 和 Claude Opus 4.8 都重度轉向「代理」能力——即不只是交談而是能執行任務的模型。
  • Multimodal Universalism: Gemini Omni points toward models that handle any input to any output seamlessly.
    多模態全能化: Gemini Omni 指向能無縫處理任何輸入到任何輸出的模型。
  • Developer Experience (DX) Focus: GitHub and independent toolmakers are focusing on "state" and "context" management to keep developers in the flow.
    專注開發者體驗 (DX): GitHub 和獨立工具開發者正專注於「狀態」和「上下文」管理,以保持開發者的專注流。

2026年5月25日 星期一

[AI] 2025-05-25 News

AI News Digest - May 25, 2026

 ai     agents    llm    anthropic  

Summary

The AI industry is entering a more mature phase focused on the practical deployment of "Agentic" systems and deep enterprise integration. Recent updates from Google DeepMind and Antigravity highlight a shift toward autonomous operating systems and models optimized for action rather than just conversation. Anthropic is doubling down on enterprise adoption through partnerships with firms like KPMG while continuing to refine its frontier model safety. Simultaneously, the community, led by figures like Simon Willison, is delivering the modular tools—such as Datasette Agent—necessary for developers to harness these powerful models in specialized environments. GitHub's recognition as a leader in AI coding agents further solidifies the trend of AI becoming a fundamental layer in the software development lifecycle. Overall, the focus has moved from "what can LLMs say" to "what can AI agents reliably do" within production workflows and corporate infrastructure.

AI 行業正進入一個更成熟的階段,重點在於「代理人」(Agentic) 系統的實際部署和深度的企業整合。Google DeepMind 和 Antigravity 最近的更新突顯了向自主操作系統和優化「行動」而非僅僅是「對話」的模型轉變。Anthropic 正在透過與 KPMG 等公司的合作加倍投入企業採用,同時持續完善其前沿模型的安全性。同時,以 Simon Willison 為首的社群正提供模組化工具(例如 Datasette Agent),讓開發者能在專業環境中運用這些強大的模型。GitHub 被認可為企業 AI 編碼代理的領導者,進一步鞏固了 AI 成為軟體開發生命週期中基礎層級的趨勢。總體而言,重點已從「LLM 能說什麼」轉向「AI 代理人在生產工作流和企業基礎設施中能可靠地做什麼」。


Anthropic

Project Glasswing: An initial update

  • Summary: An update on Anthropic's internal initiative to enhance model transparency and safety benchmarks. / 關於 Anthropic 內部旨在提高模型透明度和安全基準的倡議更新。
  • Why it matters: Safety remains a core differentiator for Anthropic as models become more autonomous. / 隨著模型變得更加自主,安全性仍然是 Anthropic 的核心競爭力。
  • Key takeaway: Continued commitment to rigorous testing before full feature rollout for frontier models. / 持續承諾在向大眾推出前沿模型的所有功能之前進行嚴格測試。

KPMG integrates Claude across its core business and workforce

  • Summary: A massive enterprise integration announcement where KPMG will deploy Claude to its thousands of employees. / 一項重大的企業整合公告,KPMG 將向其數千名員工部署 Claude。
  • Why it matters: Validates the readiness of Claude for high-stakes professional services and data-sensitive environments. / 驗證了 Claude 在高風險專業服務和數據敏感環境中的成熟度。
  • Key takeaway: Enterprise adoption is scaling rapidly, moving from pilots to core business integration. / 企業採用正在迅速擴張,從試點轉向核心業務整合。

Google DeepMind

Gemini 3.5: frontier intelligence with action

  • Summary: The latest Gemini release designed specifically to power autonomous agents with high-reliability "actions." / 最新發佈的 Gemini,專為具備高可靠「行動」能力的自主代理人提供動力。
  • Why it matters: Moves Google beyond the "chatbot" paradigm into the "agentic" software era. / 將 Google 推向超越「聊天機器人」模式,進入「代理人」軟體時代。
  • Key takeaway: A fundamental shift toward models that can interface directly with system-level tools. / 朝向能與系統級工具直接對接的模型進行根本性轉變。

Introducing Gemini Omni

  • Summary: A new multimodal model capable of processing and generating audio, video, and text in a single unified stream. / 一款全新的多模態模型,能夠在單一統一流中處理和生成音訊、影片和文字。
  • Why it matters: Enables more natural, low-latency human-AI interaction across different sensory inputs. / 實現了跨越不同感官輸入的更自然、低延遲的人機互動。
  • Key takeaway: Multimodality is becoming "omni-directional," allowing for seamless context switching. / 多模態正變得「全方位」,允許無縫的語境切換。

Simon Willison

Release datasette-agent 0.1a4

  • Summary: A new version of the AI agent for Datasette, focused on improving data exploration and tool usage. / Datasette AI 代理人的新版本,重點在於改進數據探索和工具使用。
  • Practical insight: Modular agents are easier to debug and specialize for specific datasets. / 模組化代理人更容易調試,並針對特定數據集進行專門化。
  • Useful tools or techniques mentioned: Integration with the llm library for flexible backend model switching. / 與 llm 庫整合,實現靈活的後端模型切換。

Release datasette 1.0a30

  • Summary: Significant updates to the core Datasette tool to support the growing ecosystem of AI plugins. / 對 Datasette 核心工具的重大更新,以支持不斷增長的 AI 插件生態系統。
  • Practical insight: Preparing core infrastructure is essential for the "agent-first" future of data tools. / 為「代理優先」的數據工具未來準備核心基礎設施至關重要。
  • Useful tools or techniques mentioned: Improved plugin hooks that allow agents to query and visualize data more effectively. / 改進的插件掛鉤,使代理人能更有效地查詢和視覺化數據。

Google Antigravity

Introducing Google Antigravity 2.0

  • Summary: A major release of the platform designed to build and manage fleets of AI agents. / 該平台的重大版本發佈,旨在構構建和管理 AI 代理集群。
  • Interesting innovation: Features a unified dashboard for monitoring agent health and cost across large deployments. / 具有統一的儀表板,用於跨大規模部署監控代理人的健康狀況和成本。
  • Real-world impact: Simplifies the complexity of managing multiple autonomous agents in a corporate environment. / 簡化了在企業環境中管理多個自主代理人的複雜性。

Google Antigravity Built an OS (and more)

  • Summary: An exploration of a specialized operating system built from the ground up to be AI-native. / 對從頭開始構建的 AI 原生專業操作系統的探索。
  • Interesting innovation: The OS treats the LLM as the kernel scheduler for human-system interactions. / 該操作系統將 LLM 視為人機交互的核心調度程序。
  • Real-world impact: Represents the ultimate goal of the agentic shift: an entire environment designed for AI collaboration. / 代表了代理人轉變的終極目標:為 AI 協作設計的整個環境。

GitHub Copilot

GitHub recognized as a Leader in the Gartner® Magic Quadrant™ for Enterprise AI Coding Agents

  • Summary: GitHub's platform has been officially recognized for its leadership in the emerging AI coding agent market. / GitHub 的平台在緊急的 AI 編碼代理市場中的領導地位已獲得正式認可。
  • Developer productivity impact: Confirms that Copilot is setting the standard for how AI assists in large-scale enterprise development. / 確認了 Copilot 正為 AI 如何協助大規模企業開發設定標準。
  • Important feature or workflow: Focuses on the "Agentic" features like PR summaries and automated refactors. / 重點在於「代理人」功能,如 PR 摘要和自動重構。

Building a general-purpose accessibility agent—and what we learned in the process

  • Summary: Insights from building an agent that helps developers ensure their software is accessible to everyone. / 構建一個幫助開發者確保其軟體對所有人皆可訪問的代理人的心得。
  • Developer productivity impact: Automates a historically manual and error-prone part of the QA process. / 將歷史上繁瑣且容易出錯的 QA 過程自動化。
  • Important feature or workflow: Uses vision and code analysis to proactively suggest accessibility fixes in real-time. / 利用視覺和代碼分析,即時主動地提出無障礙修復建議。

Overall Trends

  • The Rise of the OS-Agent: AI is moving from being an "app" to being the "operating system" or at least a core layer within it. / AI 正從一個「應用程序」轉向「操作系統」,或至少是其中的核心層。
  • Enterprise-Grade Reliability: The focus has shifted toward compliance, transparency, and "actionable" intelligence that businesses can trust. / 重點已轉向企業可信賴的合規性、透明度和「可操作」的智能。
  • Specialized Data Agents: Tools like Datasette Agent show that general-purpose LLMs are most effective when paired with specialized data interfaces. / 像 Datasette Agent 這樣的工具表明,通用 LLM 與專業數據接口配對時最為有效。

2026年5月19日 星期二

[Go] Concurrency Patterns - for-select loop

The for-select Loop

 golang     concurrency    for-select  

The for-select loop helps a lot in

  • Iterating something into a channel
  • Infinite loop until we tell it to stop

This example uses 2 channels, data is for sending/receiving data, done is the channel to signal the data iteration to stop in certain condition.

func IterateToChannel() {
	data := make(chan string)
	done := make(chan interface{})
	go func() {
		for v := range data {
			fmt.Print(v, " ")

			// Stop the DATA_LOOP if the value is a integer
			if _, err := strconv.Atoi(v); err == nil {
				close(done)
			}
		}
	}()

DATA_LOOP:
	for _, v := range []string{"a", "b", "1", "c"} {
		select {
		case <-done:
			break DATA_LOOP
			// return
		case data <- v:
		}
	}
}


The code prints a b 1 and stops.

Notice that in the above sample code, we can use the default case to write the incoming string to the channel data until the done channel is closed and stop the DATA_LOOP.

DATA_LOOP:
	for _, v := range []string{"a", "b", "1", "c"} {
		select {
		case <-done:
			break DATA_LOOP
        default:
            data <- v
		}
	}


Or do nothing in the default case and exit the select block.

DATA_LOOP:
	for _, v := range []string{"a", "b", "1", "c"} {
		select {
		case <-done:
			break DATA_LOOP
		default:
		}
		data <- v
	}


This example creates a infinite loop to print current time, but the loop will only run 5 times.

func InfiniteLoopThatCanStop() {
	const max int = 5
	counter := 0
	done := make(chan interface{})

LOOP:
	for {
		select {
		case <-done:
			break LOOP
		case <-time.After(1 * time.Second):
			if counter >= max {
				fmt.Println("Time's up")
				close(done)
			} else {
				// Print the current time in GMT format
				fmt.Println(time.Now().UTC().Format(http.TimeFormat))
				counter++
			}
		}
	}
}


[Go] Concurrency Patterns - Data Protected by Confinement

Data Protected by Confinement

 golang     concurrency    pattern  

Confinement ensures information is only available from one concurrent process. When this is achieved, a concurrent program is implicitly safe and no synchronization is needed.

Consider the following example.

data := []int{1, 2, 3, 4}

loopData := func(handleData chan<- int) {
	defer close(handleData)
	for _, v := range data {
		handleData <- v
	}
}

handleData := make(chan int)
go loopData(handleData)

// This line will cause deadlock
// handleData <- 5

for num := range handleData {
	fmt.Println(num)
}


If somebody adds the line handleData <- 5, the program will be deadlocked because there is another goroutine (which called loopData) was sending message to the channel handleData.

Lexical confinement can solve this problem because it uses lexical scope to expose only the correct data and concurrency primitives for multiple concurrent processes to use. It makes it impossible to do the wrong thing.

Let's update the above sample code with lexical confinemnt pattern:

data := []int{1, 2, 3, 4}

chanOwner := func() <-chan int {
	// Initiate the channel within the lexical scope of the function
	handleData := make(chan int)

	go func() {
		defer close(handleData)
		for _, v := range data {
			handleData <- v
		}
	}()
	return handleData
}

roChan := chanOwner()

for num := range roChan {
	fmt.Println(num)
}


  • The function chanOwner initiates the channel handleData in its lexical scope, and returns the readonly channel.
  • This limits the scope of write aspect of the channel to the closure that only the channel owner can write to channel, other goroutines have no chance to write but only can read from the channel.

In summary, lexical confinement has the following benefits:

  • We don't have to do the data synchorization concurrent code, because synchorization comes a cost and developer may do it wrong without any compiler error.
  • The concurrent code will be simplier to understand than without lexically confined variables and scopes.
  • Sometimes it's hard to establish confinement and we have to fall back to Go concurrency primitives.