在Go语言中,fmt软件包使用与C的printf()和scanf()函数相似的函数来实现格式化的I /O。 Go语言格式的fmt.Fprint()函数使用其操作数的默认格式并写入w。如果不将任何字符串用作参数,则在操作数之间添加空格。此外,该函数在fmt包下定义。在这里,您需要导入“fmt”包才能使用这些函数。
用法:
func Fprint(w io.Writer, a ...interface{}) (n int, err error)
参数:此函数接受两个参数,如下所示:
- 作家这是指定的标准输入或输出。
- a …interface{}:它包含一些在代码中使用的字符串或常量变量。
返回值:它返回已写入的字节数以及遇到的任何写入错误。
范例1:
// Golang program to illustrate the usage of
// fmt.Fprint() function
// Including the main package
package main
// Importing fmt and os
import (
"fmt"
"os"
)
// Calling main
func main() {
// Declaring some const variables
const name, dept = "GeeksforGeeks", "CS"
// Calling Fprint() function which returns
// "n" as the number of bytes written and
// "err" as any error ancountered
n, err:= fmt.Fprint(os.Stdout, name, " is a ",
dept, " portal.\n")
// Printing the number of bytes written
fmt.Print(n, " bytes written.\n")
// Printing if any error encountered
fmt.Print(err)
}
输出:
GeeksforGeeks is a CS portal. 30 bytes written. <nil>
范例2:
// Golang program to illustrate the usage of
// fmt.Fprint() function
// Including the main package
package main
// Importing fmt and os
import (
"fmt"
"os"
)
// Calling main
func main() {
// Declaring some const variables
const num1, num2, num3 = "a", "b", "c"
// Calling Fprint() function which returns
// "n" as the number of bytes written and
// "err" as any error encountered
n, err:= fmt.Fprint(os.Stdout, num1, num2, num3, "\n")
// Printing the number of bytes written
fmt.Print(n, " bytes written.\n")
// Printing if any error encountered
fmt.Print(err)
}
输出:
abc 4 bytes written. <nil>
在上面的代码中,使用的常量变量是字符串,因此在输出中上面显示的两个字符串之间不添加空格。
范例3:
// Golang program to illustrate the usage of
// fmt.Fprint() function
// Including the main package
package main
// Importing fmt and os
import (
"fmt"
"os"
)
// Calling main
func main() {
// Declaring some const variables
const num1, num2, num3 = 5, 15, 15
// Calling Fprint() function which returns
// "n" as the number of bytes written and
// "err" as any error encountered
n, err:= fmt.Fprint(os.Stdout, num1, num2, num3, "\n")
// Printing the number of bytes written
fmt.Print(n, " bytes written.\n")
// Printing if any error encountered
fmt.Print(err)
}
输出:
5 15 15 8 bytes written. <nil>
在上面的代码中,使用的常量变量是数字,因此在输出中上面显示的两个数字之间添加了空格。
相关用法
- Golang math.Lgamma()用法及代码示例
- Golang math.Float64bits()用法及代码示例
- Golang atomic.AddInt64()用法及代码示例
- Golang atomic.StoreInt64()用法及代码示例
- Golang reflect.FieldByIndex()用法及代码示例
- Golang string.Contains用法及代码示例
- Golang bits.Sub()用法及代码示例
- Golang io.PipeWriter.CloseWithError()用法及代码示例
- Golang time.Round()用法及代码示例
- Golang reflect.AppendSlice()用法及代码示例
- Golang reflect.ChanOf()用法及代码示例
- Golang flag.Bool()用法及代码示例
- Golang time.Sleep()用法及代码示例
- Golang time.Time.Year()用法及代码示例
- Golang reflect.DeepEqual()用法及代码示例
- Golang reflect.Indirect()用法及代码示例
- Golang reflect.CanAddr()用法及代码示例
- Golang reflect.CanInterface()用法及代码示例
- Golang reflect.CanSet()用法及代码示例
- Golang reflect.Cap()用法及代码示例
注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 fmt.Fprint() Function in Golang With Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。