Golang 技能审查

AI 智能 社区
解读按原文结构重写,命令、链接、术语均保留;右侧可核对作者原始 SKILL.md

原文结构

  • Default Stance:Unless there is a clear reason not to, prefer these defaults: Use real containers for integration tests, not mocks Prefer module-specific helpers like postgres.Run and redis.Run over lower-level generic setup when a module exists
  • Terminology: Containers Are Not Mocks:For Postgres and Redis integration tests, prefer real Postgres and Redis containers. These are not mocks. Mocks/fakes are best for unit tests and narrow seams
  • Version and API Notes:github.com/testcontainers/testcontainers-go is the main library Prefer modern Run(...) APIs GenericContainer is the older style; prefer testcontainers.Run(...) for new generic-container examples

适用与边界

  • When to Use:Testing against a real Postgres, Redis, Kafka, NATS, or other containerised dependency Verifying SQL, migrations, indexes, transactions, locking, or driver behaviour Testing cache integration against a real Redis instance
  • Anti-Patterns:Avoid these unless there is a strong reason: One heavy Postgres container per test when a package-scoped container plus data isolation would do Sharing one Postgres database across parallel tests with no transaction/schema/database isolation

原文中的明确线索

  • 要点:「real containers for integration tests」、「module-specific helpers」、「testcontainers.Run」、「testcontainers.CleanupContainer(t, ctr)」、「one container per package/test binary」、「state inside the shared container」、「per-test transactions, schemas, databases, logical Redis DBs, or key prefixes」、「random host ports」
  • 文件与命令golang-testingt.Parallel()postgres.Runredis.Runtestcontainers.RunGenericContainertestcontainers.CleanupContainer(t, ctr)github.com/testcontainers/testcontainers-go

流狐整理:以上内容来自当前 SKILL.md 的章节与原词;未补写作者没有声明的工具、兼容性或能力。

流狐档案 作者与许可取自来源;运行、权限和网络为流狐检测或估算
流狐分类
AI 智能
作者声明 Agent
未找到明确声明;不据此推断已兼容或已测试
静态检查
88 / 100 · 启发式扫描,不代表运行安全
作者 / 版本 / 许可
@tomevault-io · 未声明 license
流狐 Token 估算
低消耗
流狐接入估算
需手动接入
是否需要外部 API Key
未发现要求
检测到的系统要求
Linux · Docker
底层运行要求
Docker
检测到的文件与系统行为
  • 只读
  • 允许写入 / 修改
  • 读取环境变量
检测到的网络行为
仅限本地
安装命令数
无(仅作为资料)

档案由构建时根据 SKILL.md 与安装命令自动衍生,可能与作者实际意图存在差异。

需要注意: 未限定 allowed-tools,默认拥有全部工具权限。

输出预览 golang-testcontainers.preview
# Postgres Module Example

package repo_test

import (
    "context"
    "path/filepath"
    "testing"

    "github.com/stretchr/testify/require"

    "github.com/testcontainers/testcontainers-go"
    "github.com/testcontainers/testcontainers-go/modules/postgres"
)

func TestRepositoryWithPostgres(t *testing.T) {
    t.Parallel()

    ctx := context.Background()

    ctr, err := postgres.Run(ctx,
        "postgres:16-alpine",
        postgres.WithDatabase("app_test"),
        postgres.WithUsername("postgres"),
        postgres.WithPassword("postgres"),
        postgres.WithInitScripts(filepath.Join("testdata", "init.sql")),
        postgres.BasicWaitStrategies(),
    )
    testcontainers.CleanupContainer(t, ctr)
    require.NoError(t, err)

    dsn, err := ctr.ConnectionString(ctx, "sslmode=disable")
    require.NoError(t, err)

    _ = dsn // open your DB client here
}

讨论

基于 GitHub Discussions。登录 GitHub 即可参与讨论、点赞、订阅更新。