package
0.0.0-20240429050328-3be44a187b12
Repository: https://github.com/robertwang/golang_study.git
Documentation: pkg.go.dev

# README

Go 每日一题

今日(2023-07-31)的题目如下

下面这段代码输出什么?

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)
}
与 [2022-12-20](../../../2022/12/20/README.md) 及 [2023-04-10](../../04/10/README.md) 相同
🔑 答案解析:

参考答案及解析:South。知识点:iota 的用法、类型的 String() 方法。

根据 iota 的用法推断出 South 的值是 2;另外,如果类型定义了 String() 方法,当使用 fmt.Printf()fmt.Print()fmt.Println() 会自动使用 String() 方法,实现字符串的打印。

Reference: