Created
August 14, 2014 01:54
Revisions
-
matope created this gist
Aug 14, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ package main import ( "fmt" "time" ) func main() { fmt.Println(Greet("マトペ")) } func Greet(n string) string { t := time.Now() if 6 <= t.Hour() && t.Hour() <= 18 { return fmt.Sprintf("こんにちは%sさん。今は%d時ですよ!", n, t.Hour()) } else { return fmt.Sprintf("こんばんは%sさん。今は%d時ですよ!", n, t.Hour()) } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ func Greet(n string, t time.Time) string { if 6 <= t.Hour() && t.Hour() <= 18 { return fmt.Sprintf("こんにちは%sさん。今は%d時ですよ!", n, t.Hour()) } else { return fmt.Sprintf("こんばんは%sさん。今は%d時ですよ!", n, t.Hour()) } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ package main import ( "fmt" "time" ) var timeNowFunc = time.Now func main() { fmt.Println(Greet("マトペ")) } func Greet(n string) string { t := timeNowFunc() if 6 <= t.Hour() && t.Hour() <= 18 { return fmt.Sprintf("こんにちは%sさん。今は%d時ですよ!", n, t.Hour()) } else { return fmt.Sprintf("こんばんは%sさん。今は%d時ですよ!", n, t.Hour()) } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ package main import ( "testing" "time" ) const timeformat = "2006-01-02 15:04:06" // timeのフォーマット指定文字列 func setNow(t time.Time) { timeNowFunc = func() time.Time { return t } } func TestMain(t *testing.T) { evening, _ := time.Parse(timeformat, "2014-08-14 14:10:00") night, _ := time.Parse(timeformat, "2014-08-14 22:30:00") // 昼のテスト setNow(evening) if ret := Greet("まとぺ"); ret != "こんにちはまとぺさん。今は14時ですよ!" { t.Errorf("Greet Fails. Got [%s]\n", ret) } // 夜のテスト setNow(night) if ret := Greet("まとぺ"); ret != "こんばんはまとぺさん。今は22時ですよ!" { t.Errorf("Greet Fails. Got [%s]\n", ret) } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ package main import ( "fmt" "time" ) var _time = struct { Now func() time.Time Since func(time.Time) time.Duration }{ time.Now, time.Since, } func main() { fmt.Println(_time.Now()) }