hello

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2026 License: Apache-2.0 Imports: 3 Imported by: 1

README

tao-hello

Go Report Card GoDoc

  _   _  U _____ u  _       _       U  ___ u      _____      _      U  ___ u 
 |'| |'| \| ___"|/ |"|     |"|       \/"_ \/     |_ " _| U  /"\  u   \/"_ \/ 
/| |_| |\ |  _|" U | | u U | | u     | | | |       | |    \/ _ \/    | | | | 
U|  _  |u | |___  \| |/__ \| |/__.-,_| |_| |      /| |\   / ___ \.-,_| |_| | 
 |_| |_|  |_____|  |_____| |_____|\_)-\___/      u |_|U  /_/   \_\\_)-\___/  
 //   \\  <<   >>  //  \\  //  \\      \\        _// \\_  \\    >>     \\    
(_") ("_)(__) (__)(_")("_)(_")("_)    (__)      (__) (__)(__)  (__)   (__)  

Tao Universe 示例组件,演示如何创建自定义 Tao 单元。适合作为新组件开发的参考模板。

安装

go get github.com/taouniverse/tao-hello

使用

导入
import _ "github.com/taouniverse/tao-hello"
配置

单实例模式:

hello:
  print: "Hello, Tao!"
  times: 3
  run_after: []

多实例模式:

hello:
  default_instance: greeting
  greeting:
    print: "Hello, Tao!"
    times: 3
  farewell:
    print: "Goodbye, Tao!"
    times: 1

工厂模式

API 说明
hello.Factory *tao.BaseFactory[struct{}] 工厂实例

作为模板

tao-hello 是创建新 Tao 组件的最佳参考。核心结构如下:

tao-hello/
├── config.go          # Config 接口实现 + InstanceConfig 定义
├── hello.go           # init() 注册 + 构造器函数
├── config_test.go     # 配置解析测试
└── hello_test.go      # 集成测试

关键模式:

  1. 两层 ConfigInstanceConfig 存储具体字段,Config 嵌入 BaseMultiConfig[InstanceConfig]
  2. 构造器NewHello(name string, cfg InstanceConfig) (instance, closer, error) — 返回三元组
  3. init() 注册:通过 tao.Register(ConfigKey, &Config{}, NewHello) 将组件注册到 Tao Universe
  4. 便捷函数(可选):提供 Xxx()GetXxx(name) 快捷访问默认/指定实例
最小化模板代码
// config.go
type InstanceConfig struct {
    // 你的配置字段
}

type Config struct {
    tao.BaseMultiConfig[InstanceConfig]
    RunAfters []string `json:"run_after,omitempty"`
}

func (c *Config) Name() string        { return ConfigKey }
func (c *Config) ValidSelf()          { /* 设置默认值 */ }
func (c *Config) ToTask() tao.Task     { /* 任务逻辑 */ }
func (c *Config) RunAfter() []string   { return c.RunAfters }
// xxx.go
var M = &Config{}
var Factory *tao.BaseFactory[*YourType]

func init() {
    var err error
    Factory, err = tao.Register(ConfigKey, M, NewXxx)
    if err != nil {
        panic(err.Error())
    }
}

func NewXxx(name string, cfg InstanceConfig) (*YourType, func() error, error) {
    // 创建实例
    instance := createInstance(cfg)
    
    closer := func() error {
        // 清理资源
        return nil
    }
    
    return instance, closer, nil
}

// 可选:便捷函数
func Xxx() (*YourType, error) {
    return Factory.Get(M.GetDefaultInstanceName())
}

func GetXxx(name string) (*YourType, error) {
    return Factory.Get(name)
}

单元测试

测试文件 说明 运行条件
config_test.go Config 解析与默认值验证 无需外部依赖
hello_test.go 完整集成测试 无需外部服务
运行单元测试
# 全部测试可直接运行
go test -v ./...

Documentation

Index

Constants

View Source
const ConfigKey = "hello"

ConfigKey for this repo

Variables

View Source
var Factory *tao.BaseFactory[struct{}]

Factory is the global factory instance

View Source
var H = &Config{}

H is the global config instance for tao-hello

Functions

func NewHello added in v0.2.0

func NewHello(name string, config InstanceConfig) (struct{}, func() error, error)

NewHello creates a new Hello instance for factory pattern

Types

type Config added in v0.1.0

type Config struct {
	tao.BaseMultiConfig[InstanceConfig]
	RunAfters []string `json:"run_after,omitempty" yaml:"run_after,omitempty"`
}

Config 总配置,实现 tao.MultiConfig 接口

func (*Config) Name added in v0.1.0

func (h *Config) Name() string

Name of Config

func (*Config) RunAfter added in v0.1.0

func (h *Config) RunAfter() []string

RunAfter defines pre task names

func (*Config) ToTask added in v0.1.0

func (h *Config) ToTask() tao.Task

ToTask transform itself to Task

func (*Config) ValidSelf added in v0.1.0

func (h *Config) ValidSelf()

ValidSelf with some default values

type InstanceConfig added in v0.2.0

type InstanceConfig struct {
	Print string `json:"print"`
	Times int    `json:"times"`
}

InstanceConfig 单实例配置

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL