博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
我被阿里云美女清宵的观后感给撩了
阅读量:5898 次
发布时间:2019-06-19

本文共 3622 字,大约阅读时间需要 12 分钟。

这是她看完药神之后的观后感,看到chanel的声明,就吸引我一直把它读完。

package mainimport (    "fmt"    "math/rand"    "sync"    "time")var (    r = rand.New(rand.NewSource(time.Now().Unix()))    disasterSignal = make(chan string)    accidentSignal = make(chan string)    diseaseSignal  = make(chan string))// Element : abstract factor which life consisted bytype Element interface {    Improve()    Depress()    Stable()    Enable() bool    BeAbleHandle(event string) bool}type Activity interface {    IsSuitable(life *Life) bool    Do(life *Life)    Interrupted()}type Life struct {    Sex string    Age time.Duration    Health       Element    Knowledge    Element    Ability      Element    RelationShip Element    Wealth       Element    OtherElement Element    Work        Activity    Study       Activity    Exercise    Activity    Entertain   Activity    Rest        Activity    OtherActive Activity    isDoings []Activity    vitalitySignal chan struct{}    NaturalDeath   chan struct{}}func (f *Life) Join(oppositeSex *Life, love, family Element) (*Life, error) {    if !love.Enable() || !family.Enable() || f.Sex == oppositeSex.Sex {        return nil, fmt.Errorf("Sorry, no boby should be borned!")    }    boby := &Life{        Sex:            []string{"male", "female"}[r.Intn(2)],        Age:            0,        isDoings:       []Activity{},        NaturalDeath:   make(chan struct{}),        vitalitySignal: make(chan struct{}),    }    return boby, nil}func (f *Life) Run() {    go ExternalEndanger(f)    // time elapses day by day    for {        startTime := time.Now().UTC()        wg := &sync.WaitGroup{}        for _, active := range []Activity{f.Study, f.Work, f.Entertain, f.Exercise, f.Rest, f.OtherActive} {            if f.SuitableFor(active) {                wg.Add(1)                go func(activity Activity) {                    defer wg.Wait()                    activity.Do(f)                }(active)            }        }        select {        case <-f.NaturalDeath:            f.Close()            fmt.Println("Life is short, make it colourful and cherish the love around all!")            return        case <-f.vitalitySignal:            fmt.Println("记得买保险!")            return        case <-time.After(24*time.Hour - time.Now().UTC().Sub(startTime)):            fmt.Println("One day went by...")        }        //wg.Wait()        f.Age += 24 * time.Hour    }    fmt.Println("Goodbye, life!")}func (f *Life) Somehow() {    // happened something to effect one to reach some life stage}func (f *Life) SuitableFor(active Activity) bool {    return active.IsSuitable(f)}func (f *Life) Survive(event string) bool {    for _, e := range []Element{f.Health, f.Knowledge, f.Ability, f.RelationShip, f.Wealth, f.OtherElement} {        if !e.BeAbleHandle(event) {            return false        }    }    return true}func (f *Life) Close() {    for _, doing := range f.isDoings {        doing.Interrupted()    }    close(f.vitalitySignal)}var female = LifeFromSomeWhere("female")var male = LifeFromSomeWhere("male")func ExternalEndanger(f *Life) {    for {        event := ""        select {        case event = <-diseaseSignal:        case event = <-disasterSignal:        case event = <-accidentSignal:        }        if !f.Survive(event) {            f.Close()            return        }    }}func LifeFromSomeWhere(sex string) *Life {    life := &Life{Sex: sex}    life.Somehow()    return life}func main() {    // I don't know the question of "鸡生蛋 or 蛋生鸡"...    newLife, err := female.Join(male, ElementImp{Type: "love"}, ElementImp{Type: "family"})    if err == nil {        newLife.Run()    }}

原贴地址是阿里云云栖社区,拼命三郎OL引用自

转载地址:http://wxasx.baihongyu.com/

你可能感兴趣的文章
delphi基本语法
查看>>
沙盒目录介绍
查看>>
260. Single Number III
查看>>
Hadoop生态圈-Kafka的完全分布式部署
查看>>
css的border的solid
查看>>
[MODx] Build a CMP (Custom manager page) using MIGX in MODX 2.3 -- 1
查看>>
jQuery自动完成点击html元素
查看>>
[算法]基于分区最近点算法的二维平面
查看>>
webpack多页应用架构系列(七):开发环境、生产环境傻傻分不清楚?
查看>>
笨办法学C 练习1:启用编译器
查看>>
树的总结--树的性质(树的深度) leetcode
查看>>
nagios短信报警(飞信fetion20080522004-linrh4)
查看>>
【Android游戏开发之六】在SurfaceView中添加组件!!!!并且相互交互数据!!!!...
查看>>
linux 将大文件分成小文件
查看>>
CCNA- 距离矢量路由协议学习
查看>>
企业实践用户邮箱导入/导出(第2部分)
查看>>
我的友情链接
查看>>
如何学习Linux命令-初级篇
查看>>
从Oracle Public Yum为Oracle Linux建立本地的Yum源
查看>>
在 SELECT 查询中使用表表达式
查看>>