【发布时间】:2016-05-08 16:37:03
【问题描述】:
我是 Go 语言的新生,我想问一些基本的事情,我们如何才能使这个函数有意义。
我们需要使用“strconv”来解决这个问题。
package main
import (
"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")
}
输出应该是这样的
Start of session
I am Student0
I am Student1
I am Student2
I am Student3
I am Student4
I am Student5
End of session
Student5 Leaving
Student4 Leaving
Student3 Leaving
Student2 Leaving
Student1 Leaving
Student0 Leaving
Room Empty
我们只需要编写一个 main function() 和一个简单的 for 循环即可获得结果。
【问题讨论】:
-
我们只需要写一个main function()和一个简单的for循环就可以得到结果
-
我对此一无所知,我正在考虑使用 defer 并放入 for 循环,但它无法正常工作。
-
我们在这个程序中真的需要“strconv”吗?我想我们真的不需要这个导入。
-
@MR.S:对于大于一位十进制数字的数字,您需要
strconv。 -
停止将旧问题编辑成完全不同的问题。如果您有新问题,请发布新问题。
标签: go