如何在Go语言中打印“Hello, World!”100次?

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

How do I print "Hello, World!" 100 times in Go?

问题

我到目前为止有这个,但是我不确定要添加什么来使它打印出“Hello, World!”100次...我该怎么做?

  1. package main
  2. import "fmt"
  3. func main() {
  4. for i := 0; i < 100; i++ {
  5. fmt.Println("Hello, World!")
  6. }
  7. }
英文:

I have this so far, but I'm not sure what to add to make it print "Hello, World!" 100 times... How can I do it?

  1. package main
  2. import &quot;fmt&quot;
  3. func main() {
  4. fmt.Println(&quot;Hello, World!&quot;)
  5. }

答案1

得分: 4

这将类似于(假设其他代码是正确的):

  1. package main
  2. import "fmt"
  3. func main() {
  4. for i := 0; i < 100; i++ {
  5. fmt.Println("你好世界")
  6. }
  7. }
英文:

That would be something like (assuming the other code is correct):

  1. package main
  2. import &quot;fmt&quot;
  3. func main() {
  4. for i := 0; i &lt; 100; i++ {
  5. fmt.Println(&quot;Hello world&quot;)
  6. }
  7. }

答案2

得分: 4

你应该绝对使用*strings.Repeat*:

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. fmt.Print(strings.Repeat("Hello, World!\n", 100))
  8. }
英文:

You should definitely use strings.Repeat:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;strings&quot;
  5. )
  6. func main() {
  7. fmt.Print(strings.Repeat(&quot;Hello, World!\n&quot;, 100))
  8. }

huangapple
  • 本文由 发表于 2013年4月26日 10:19:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/16227730.html
匿名

发表评论

匿名网友

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

确定