首页 > 其他 > 详细

关于kratos的底层赋值参考

时间:2021-03-30 20:35:32      阅读:27      评论:0      收藏:0      [点我收藏+]
package main

import (
    "fmt"
    "time"
)

func main() {
    s := NewServices(
        SetName("peter"),
        SetTimeout(time.Second*5),
    )

    fmt.Println("name:", s.conf.Name)
    fmt.Println("time", s.conf.Timeout)
}

type Option func(options *Config)

type Config struct {
    Name    string
    Timeout time.Duration
}

type Services struct {
    conf Config
}

func SetTimeout(t time.Duration) Option {
    return func(options *Config) {
        options.Timeout = t
    }
}

func SetName(name string) Option {
    return func(options *Config) {
        options.Name = name
    }
}

func NewServices(opts ...Option) Services {
    c := Config{}
    for _, op := range opts {
        op(&c)
    }
    s := Services{}
    s.conf = c
    return s
}

 

关于kratos的底层赋值参考

原文:https://www.cnblogs.com/chongyao/p/14597998.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!