golang fmt.Sprint()串联输出
fmt.Sprint()语法
func fmt.Sprint(a ...any) string
golang源码对fmt.Sprint()方法的介绍
Sprint formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string.
即,golang的fmt标准库中的Sprint采用默认格式串联所有参数,并返回一个字符串,假如两个相邻的参数都不是字符串,那么Sprint()方法将会在二者的输出之间添加空格。
fmt.Sprint()参数
参数 | 描述 |
---|---|
a | (个数)可变的参数,用于格式化输出; |
fmt.Sprint()方法返回值
string字符串
fmt.Sprint()方法实例代码
package main
import (
"fmt"
)
func main() {
name := "xiaoming"
age := "20"
str := fmt.Sprint(name, age, "岁")
fmt.Print(str)
}
运行go文件,得到输出:
xiaoming20岁
免责声明:内容仅供参考。