為程序員的愛情點贊
程序員的愛簡單實用,就像他們的代碼,過程雖不同,也曾拋出異常,但都是 happy ending。當情人節遇上元宵節,run 愛情密碼程序并轉發本文至微信朋友圈,就能為你及好友帶來愛情蜜運。
下面的程序代表程序員的愛情密碼,不浮夸,簡單實用。來吧,進入:http://play.golang.org/p/xl1P6jYtIZ
package main
import (
"fmt" "math/rand" "time"
)
func main () {
msg := make (chan string)
end := make (chan string)
go girlStory (msg, end)
go boyStory (msg, end)
fmt.Println (<- end)
}
func boyStory (msg, end chan string) {
sendMsg ("boy", "girl", "hi!", "happy", msg)
for {
switch m := <- msg; {
case m == "hi!": sendMsg ("boy", "girl", "hi!", "miss", msg)
case m == "...": sendMsg ("boy", "girl", "hi!", "angry", msg)
case m == "hi~": sendMsg ("boy", "girl", "a u happy?", "exciting", msg)
case m == "yes, i am happy": sendMsg ("boy", "girl", "ok,nice", "so sad", msg)
case m == "i am lonely...": sendMsg ("boy", "girl", "be my love?", "exciting!", msg)
case m == "ok, why not": end <- "---May happy ending of love belong to every developer. ---"
}
time.Sleep (time.Duration (1e9))
}
}
func girlStory (msg, end chan string) {
rand.Seed (time.Now () .UnixNano ())
for {
happy := rand.Intn (5) < 3
alone := rand.Intn (5) == 0 switch m := <- msg; {
case m == "hi!":
if happy {
sendMsg ("girl", "boy", "hi~", "happy", msg)
} else {
sendMsg ("girl", "boy", "hi!", "sad", msg)
}
case m == "a u happy?":
if !happy && alone {
sendMsg ("girl", "boy", "i am lonely...", "sad", msg)
} else if !happy {
sendMsg ("girl", "boy", "...", "sad", msg)
} else {
sendMsg ("girl", "boy", "yes, i am happy", "happy", msg)
}
case m == "ok,nice":
sendMsg ("girl", "boy", "...", "-_-", msg)
case m == "be my love?":
sendMsg ("girl", "boy", "ok, why not", "happy", msg)
}
time.Sleep (time.Duration (1e9))
}
}
func sendMsg (from, to, body, feeling string, msg chan string) {
fmt.Println (from, "(", feeling, "): ", body)
msg <- body
}
點擊 run 按鈕,祝你們愛情甜蜜~~(還有愛情程序文檔詳細說明哦)
boy ( happy ): hi!
girl ( happy ): hi~
boy ( exciting ): a u happy?
girl ( happy ): yes, i am happy
boy ( so sad ): ok,nice
girl ( -_- ): ...
boy ( angry ): hi!
girl ( sad ): hi!
boy ( miss ): hi!
girl ( sad ): hi!
boy ( miss ): hi!
girl ( happy ): hi~
boy ( exciting ): a u happy?
girl ( sad ): i am lonely...
boy ( exciting! ): be my love?
girl ( happy ): ok, why not
---May happy ending of love belong to every developer. ---

</div>