golang 文本叠加
您可以使用 fmt.Sprintf 和 + 运算符将文本叠加起来。
下面是一个示例代码:
package main
import "fmt"
func main() {
name := "John"
age := 30
profession := "programmer"
text := fmt.Sprintf("My name is %s, I'm %d years old and I work as a %s.", name, age, profession)
fmt.Println(text)
}
输出:
My name is John, I'm 30 years old and I work as a programmer.
在上面的示例中,我们使用 fmt.Sprintf 将变量 name、age 和 profession 插入到字符串中。然后使用 + 运算符将生成的字符串与其他文本连接起来。
原文地址: https://www.cveoy.top/t/topic/eoa6 著作权归作者所有。请勿转载和采集!