本文主要是介绍【程序媛晒83行代码】人生如码,用代码构造极其简单人生模型的清宵小姐姐...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在中国程序媛中,他们的代码有什么样的魅力,Aone联合云栖社区、饿了么、钉钉、阿里云、天猫、口碑发起首届程序媛比码活动——不秀大长腿,秀高智商;不秀美图照,秀代码图,参与晒码互动游戏赢“83行代码”T恤!
我们来说说这群女工程师的第83行代码及代码背后的故事:
清宵:人生如码
我是阿里云函数计算团队技术专家清宵,用动图撩一下大家吧~~大家好!
我的第83行代码来自这个简单的Life模型,非项目非社区贡献,纯业余时间自娱自乐。
看了《药神》之后,对人生的感悟加深。回来没有写什么观后感,却是随便写了一段代码,构造一个极其简单的人生模型,算作总结思考了。
人生如码,以梦为马。珍惜所爱,精彩过好每一天!
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 by
type Element interface {Improve()Depress()Stable()Enable() boolBeAbleHandle(event string) bool
}type Activity interface {IsSuitable(life *Life) boolDo(life *Life)Interrupted()
}type Life struct {Sex stringAge time.DurationHealth ElementKnowledge ElementAbility ElementRelationShip ElementWealth ElementOtherElement ElementWork ActivityStudy ActivityExercise ActivityEntertain ActivityRest ActivityOtherActive ActivityisDoings []ActivityvitalitySignal 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 dayfor {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!")returncase <-f.vitalitySignal:fmt.Println("记得买保险!")returncase <-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()}
}
与清宵小姐姐互动,为她打call——>点击进入清宵晒码
这篇关于【程序媛晒83行代码】人生如码,用代码构造极其简单人生模型的清宵小姐姐...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!