package
0.0.0-20240429050328-3be44a187b12
Repository: https://github.com/robertwang/golang_study.git
Documentation: pkg.go.dev
# README
Go每日一题
今日(2022-12-20) 的题目如下
下面这段代码输出什么?
type Direction int
const (
North Direction = iota
East
South
West
)
func (d Direction) String() string {
return [...]string{"North", "East", "South", "West"}[d]
}
func main() {
fmt.Println(South)
}
🔑 答案解析:
参考答案及解析:South。知识点:iota 的用法、类型的 String() 方法。
根据 iota 的用法推断出 South 的值是 2;另外,如果类型定义了 String() 方法,当使用 fmt.Printf()、fmt.Print() 和 fmt.Println() 会自动使用 String() 方法,实现字符串的打印。
Reference:
https://wiki.jikexueyuan.com/project/the-way-to-go/10.7.html https://www.sunansheng.com/archives/24.html https://yourbasic.org/golang/iota/