這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
package main
import ( "fmt" "os")
type tt struct { //定義資料結構 tt,用於存放每日的課程 One string //0 Two string //1 Three string //2 Four string //3 } type DAY struct { //定義資料結構 DAY ,用於存放一周的,英文簡寫 week string }
func main() { //主函數 var weekday int //定義一個int 類型的值,weekday //fmt.Printf("Enter the weekday : ") //fmt.Scanf("%d",&weekday) weekday = getday() //從getday()函數中得傳回值,(如,1,2,3,....7) print_tt(weekday) //把從getday()函數中得到的傳回值,傳送給print_tt()函數,用於顯示一周中,特定的星期的課程 }
func print_tt(weekday int) { //用於顯示每日的課程
DAYS := DAY{"Mon, Tus, Wed, Thu , Fri , Sat , Sun"} //初始化資料結構DAY,值為DAYS,內容為"Mon,Tus,..." Monday := tt{"1-2,1-604,English" ,"5-6,3-303,MacroEco", "7-8,1-501,Market" , " "} //定義Monday為tt結構類型,並初始化,下面的理解一樣Tusday := tt{"3-4,Ground,Physical" , "5-6,1-508,Social Market" ,"7-8,(11,15,19),3-308,Xingxi" ," "}Wednesday := tt{"3-4,1-604,English" , "5-6,1-504,Tongjixue" ," " , " "}Thusday := tt{"1-2,English Lisen" , "3-4,5-201,Internation Theory" ," " , " "}Friday := tt{"3-4,3-209,MaoZheDong" , "5-6,1-504,TongjiXue" ," ", " "} var get_tt int get_tt = weekday //下面的如果為判斷輸入的數字為多少,並分開了兩個部分進行比較 if get_tt > 3 && get_tt < 7 { //第一部分, 3-7 for i :=3 ;i <= get_tt ; i++ { if i == get_tt { switch i { case 4: fmt.Printf("%s\n%s\n \n",Thusday.One,Thusday.Two) case 5: fmt.Printf("%s\n%s\n \n",Friday.One,Friday.Two) case 6: fmt.Printf("Holiday Now") // case 7: fmt.Printf("Holiday now\n") } } } } else if get_tt <=3 && get_tt > 0 { //第二部分,0-3 for j := 0 ; j <= get_tt ; j++ { if j == get_tt { switch j { case 1: fmt.Printf("%s\n%s\n%s\n \n",Monday.One,Monday.Two,Monday.Three) case 2: fmt.Printf("%s\n%s\n%s\n \n",Tusday.One,Tusday.Two,Tusday.Three) case 3: fmt.Printf("%s\n%s\n \n",Wednesday.One,Wednesday.Two) } } }} else if get_tt == 8 { //第三部分是,當get_tt = 8,這個值是為getday()函數預設得到的 fmt.Printf("Please input the weekday name\n") fmt.Printf("Example: timetable Mon or timetable.exe Mon\n") fmt.Printf("weekday define as belows\n") fmt.Printf("%s\n",DAYS.week)} else { fmt.Printf("Input Error")}} func getday() int { //此函數是將程式運行時,輸入的參數改為數字形式,再返回 //函數的原型為fnuc getday() int ,其中getday()為函數,而這個int為傳回值的類型。 if len(os.Args) > 1 { /*os.Args[0] is "timetable" or "timetable.exe" */ if os.Args[1] == "Mon" { return 1 } if os.Args[1] == "Tus" { return 2 } if os.Args[1] == "Wed" { return 3 } if os.Args[1] == "Thu" { return 4 } if os.Args[1] == "Fri" { return 5 } if os.Args[1] == "Sat" || os.Args[1] == "Sun" { return 6 }} return 8 //當沒有輸入參數或者輸入的參數,與給出的值,不相同 //就會返回8,這就是在print_tt()函數中的 get_tt = 8,的原因了。}