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

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

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

问题

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

package main

import "fmt"

func main() {
    for i := 0; i < 100; i++ {
        fmt.Println("Hello, World!")
    }
}
英文:

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?

package main

import &quot;fmt&quot;

func main() {
    fmt.Println(&quot;Hello, World!&quot;)
}

答案1

得分: 4

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

package main
import "fmt"
func main() {
    for i := 0; i < 100; i++ {
        fmt.Println("你好世界")
    }
}
英文:

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

package main
import &quot;fmt&quot;
func main() {
    for i := 0; i &lt; 100; i++ {
        fmt.Println(&quot;Hello world&quot;)
    }
}

答案2

得分: 4

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

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Print(strings.Repeat("Hello, World!\n", 100))
}
英文:

You should definitely use strings.Repeat:

package main

import (
    &quot;fmt&quot;
    &quot;strings&quot;
)

func main() {
    fmt.Print(strings.Repeat(&quot;Hello, World!\n&quot;, 100))
}

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:

确定