我是 Go 語言的大一新生,我想問一些基本的事情,我們?nèi)绾尾拍苁惯@個(gè)功能有意義。我們需要使用“strconv”來解決這個(gè)問題。 package mainimport ( "fat" "strconv")type Student struct { Name string}func (stu *Student) Leave() { fmt.Println(stu.Name + " Leaving")}func (stu *Student) Present() { fmt.Println("I am " + stu.Name)}func main() { fmt.Println("Start of session") for i := 0; i < 6; i++ { s := Student{Name: fmt.Sprintf("Student%d", i)} s.Present() fmt.Println("Room Empty") defer s.Leave()} fmt.Println("End of session")} 輸出應(yīng)該是這樣的Start of session I am Student0I am Student1I am Student2I am Student3I am Student4I am Student5 End of session Student5 Leaving Student4 Leaving Student3 Leaving Student2 Leaving Student1 Leaving Student0 Leaving Room Empty我們只需要編寫一個(gè) main function() 和一個(gè)簡(jiǎn)單的 for 循環(huán)來獲得結(jié)果。
Go:基本的 for 循環(huán)和 strconv
阿波羅的戰(zhàn)車
2021-12-07 16:53:50