go语言之以x因子的倍数重复一个字符
要在 Golang 中创建一个字母的多个实例/重复,您必须使用 strings.Repeat() 函数。
例子:
package main
import (
"fmt"
"strings"
)
func main() {
str := []string{"a", "b", "c", "d", "e"}
fmt.Println(strings.Join(str, " "))
// Python way of repeating string by multiple of x will
// not work here
//str2 := []string{"a" * 2, "b", "c", "d", "e"}
// Golang's way of multiplying a string by x factor
str2 := []string{strings.Repeat("a", 2), "b", "c", "d", "e"}
fmt.Println(strings.Join(str2, " "))
a2 := strings.Repeat("a", 2)
b3 := strings.Repeat("b", 3)
c4 := strings.Repeat("c", 4)
d5 := strings.Repeat("d", 5)
e9 := strings.Repeat("e", 9)
str3 := []string{a2, b3, c4, d5, e9}
fmt.Println(strings.Join(str3, " "))
}
测试输出
a b c d e
aa b c d e
aa bbb cccc ddddd eeeeeeeee
相关文章