What is an idiomatic way to enumerate the alphabet in go?

huangapple go评论78阅读模式
英文:

What is an idiomatic way to enumerate the alphabet in go?

问题

这可能是最好的方法:

for i := 'a'; i <= 'z'; i++ {
    fmt.Println(string(i))
}

有更好/更符合惯用法的方法吗?

英文:

This might be the best way:

for i := &#39;a&#39;; i &lt;= &#39;z&#39;; i++ {
    fmt.Println(string(i))
}

Is there a better / idiomatic approach?

答案1

得分: 4

以下是您要翻译的内容:

for _, c := range "abcdefghijklmnopqrstuvwxyz" {
  fmt.Println(string(c))
}

翻译结果:

for _, c := range "abcdefghijklmnopqrstuvwxyz" {
  fmt.Println(string(c))
}

请注意,这是您提供的代码片段的翻译结果,没有其他额外的内容。

英文:
for _, c := range &quot;abcdefghijklmnopqrstuvwxyz&quot; {
  fmt.Println(string(c))
}

答案2

得分: 1

这个问题的答案很可能大部分基于个人观点,所以不太适合在stackoverflow上提问,然而你描述的方式确实可以作为在Go语言中处理小写英文字母的最佳解决方案。对于其他字母表来说,制定规则可能会更加复杂。

英文:

This question's answers will arguably be mostly opinion-based so not really a good fit for stackoverflow, yet the way you describe is indeed acceptable as an optimal solution for the lower-case english alphabet in idiomatic Go. Other alphabets will be more complicated to formulate.

答案3

得分: 0

没有更好/更惯用的方法吗?

英文:

> Is there a better / idiomatic approach?

No.

答案4

得分: 0

你可以尝试以下代码:

for i := 97; i < 123; i++ {
    fmt.Printf("%c", i)
}

在线演示

英文:

You can try

for i := 97 ; i &lt; 123 ; i++ {
	fmt.Printf(&quot;%c&quot;,i)
}

Live Demo

huangapple
  • 本文由 发表于 2013年9月30日 11:58:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/19086263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定