栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

golang中的print系函数详解

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

golang中的print系函数详解

golang中的print系函数详解

pirnt系函数来自fmt包,主要用于做各种格式的输出
这些函数主要有

  1. fmt.Fprintf
  2. fmt.Printf
  3. fmt.Sprintf
  4. fmt.Fprint
  5. fmt.Print
  6. fmt.Sprint
  7. fmt.Fprintln
  8. fmt.Println
  9. fmt.Sprintln
  10. 总结

下面来逐个分析

import (
    "fmt"
    "os"
    "io"
)
fmt.Fprintf
  • 函数原型:
Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)
  • 官方注释
    Fprintf formats according to a format specifier and writes to w.It returns the number of bytes written and any write error encountered.

  • Arguement
    fmt.Fprintf()依据指定的格式向第一个参数内写入字符串,第一参数必须实现了 io.Writer接口。Fprintf()能够写入任何类型,只要其实现了 Write方法,包括 os.Stdout,文件(例如 os.File),管道,网络连接,通道等等,同样的也可以使用 bufio包中缓冲写入。bufio 包中定义了 type Writer struct{...}。Fprintf:来格式化并输出到 io.Writers 而不是 os.Stdout。

// example
func ExampleFprintf() {
	const name, age = "Kim", 22
	n, err := fmt.Fprintf(os.Stdout, "%s is %d years old.n", name, age)

	// The n and err return values from Fprintf are
	// those returned by the underlying io.Writer.
	if err != nil {
		fmt.Fprintf(os.Stderr, "Fprintf: %vn", err)
	}
	fmt.Printf("%d bytes written.n", n)

	// Output:
	// Kim is 22 years old.
	// 21 bytes written.
}
ExampleFprintf()
Kim is 22 years old.
21 bytes written.
fmt.Printf
  • 函数原型
Printf(format string, a ...interface{}) (n int, err error)
  • 官方注释
    Printf formats according to a format specifier and writes to standard output.It returns the number of bytes written and any write error encountered.

  • Arguement
    只可以打印出格式化的字符串。只可以直接输出字符串类型的变量(不可以输出整形变量和整形 等),其实这个函数是fmt.Fprintf的调用,其调用语句return Fprintf(os.Stdout, format, a...),输出对象为标准输出。

  • optional

optionaldescription
%d十进制整数
%x, %o, %b十六进制,八进制,二进制整数
%f, %g, %e浮点数,3.141593 3.141592653589793 3.141593e+00
%t布尔 ture或false
%c字符(rune)(Uniode码点)
%s字符串
%q带双引号的字符串"abc"或带单引号的字符’c’
%v变量的自然形式
%T变量的类型
%%字面上的百分号标志(无操作数)
fmt.Printf("整数:%dt浮点数:%vt字符串:%sn", 1, 3.14, "hello world")
func ExamplePrintf() {
	const name, age = "Kim", 22
	fmt.Printf("%s is %d years old.n", name, age)

	// It is conventional not to worry about any
	// error returned by Printf.

	// Output:
	// Kim is 22 years old.
}
ExamplePrintf()
整数:1	浮点数:3.14	字符串:hello world
Kim is 22 years old.
fmt.Sprintf
  • 函数原型
func Sprintf(format string, a ...interface{}) string
  • 官方注释
    Sprintf formats according to a format specifier and returns the resulting string.

  • Argument
    格式化并返回一个字符串而不带任何输出。可用于给其他函数传递格式化参数。

func ExampleSprintf() {
	const name, age = "Kim", 22
	s := fmt.Sprintf("%s is %d years old.n", name, age)

	io.WriteString(os.Stdout, s) // Ignoring error for simplicity.

	// Output:
	// Kim is 22 years old.
}
ExampleSprintf()
Kim is 22 years old.
fmt.Fprint
  • 函数原型
Fprint(w io.Writer, a ...interface{}) (n int, err error)
  • 官方注释
    Fprint formats using the default formats for its operands and writes to w.Spaces are added between operands when neither is a string.It returns the number of bytes written and any write error encountered.

  • Argument
    向io.Writer对象中输入字符,第一个参数为输入对象,后面的参数为字符串,直接输出

func ExampleFprint() {
	const name, age = "Kim", 22
	n, err := fmt.Fprint(os.Stdout, name, " is ", age, " years old.n")

	// The n and err return values from Fprint are
	// those returned by the underlying io.Writer.
	if err != nil {
		fmt.Fprintf(os.Stderr, "Fprint: %vn", err)
	}
	fmt.Print(n, " bytes written.n")

	// Output:
	// Kim is 22 years old.
	// 21 bytes written.
}
ExampleFprint()
Kim is 22 years old.
21 bytes written.
fmt.Print
  • 函数原型
func Print(a ...interface{}) (n int, err error) {
	return Fprint(os.Stdout, a...)
}
  • 官方注释
    Print formats using the default formats for its operands and writes to standard output.Spaces are added between operands when neither is a string.It returns the number of bytes written and any write error encountered.

  • Arguement
    显然,这个函数是对fmt.Fprint的调用,只是把输入对象默认为标准输出。

func ExamplePrint() {
	const name, age = "Kim", 22
	fmt.Print(name, " is ", age, " years old.n")

	// It is conventional not to worry about any
	// error returned by Print.

	// Output:
	// Kim is 22 years old.
}
ExamplePrint()
Kim is 22 years old.
fmt.Sprint
  • 函数原型
Sprint(a ...interface{}) string  
  • 官方注释
    Sprint formats using the default formats for its operands and returns the resulting string.Spaces are added between operands when neither is a string.

  • Arguement
    格式化并返回一个字符串而不带任何输出

func ExampleSprint() {
	const name, age = "Kim", 22
	s := fmt.Sprint(name, " is ", age, " years old.n")

	io.WriteString(os.Stdout, s) // Ignoring error for simplicity.

	// Output:
	// Kim is 22 years old.
}
ExampleSprint()
Kim is 22 years old.
fmt.Fprintln
  • 函数原型
Fprintln(w io.Writer, a ...interface{}) (n int, err error)
  • 官方注释
    Fprintln formats using the default formats for its operands and writes to w.Spaces are always added between operands and a newline is appended.It returns the number of bytes written and any write error encountered.

  • Arguement
    输入字符串到io.Writer,并且带有换行

func ExampleFprintln() {
	const name, age = "Kim", 22
	n, err := fmt.Fprintln(os.Stdout, name, "is", age, "years old.")

	// The n and err return values from Fprintln are
	// those returned by the underlying io.Writer.
	if err != nil {
		fmt.Fprintf(os.Stderr, "Fprintln: %vn", err)
	}
	fmt.Println(n, "bytes written.")

	// Output:
	// Kim is 22 years old.
	// 21 bytes written.
}
ExampleFprintln()
Kim is 22 years old.
21 bytes written.
fmt.Println
  • 函数原型
func Println(a ...interface{}) (n int, err error) {
	return Fprintln(os.Stdout, a...)
}
  • 官方注释
    Println formats using the default formats for its operands and writes to standard output.Spaces are always added between operands and a newline is appended.It returns the number of bytes written and any write error encountered.

  • Arguement
    显然这个函数是对fmt.Fprintln的调用,输入到os.Stdout

func ExamplePrintln() {
	const name, age = "Kim", 22
	fmt.Println(name, "is", age, "years old.")

	// It is conventional not to worry about any
	// error returned by Println.

	// Output:
	// Kim is 22 years old.
}
ExamplePrintln()
Kim is 22 years old.
fmt.Sprintln
  • 函数原型
Sprintln(a ...interface{}) string
  • 官方注释
    Sprintln formats using the default formats for its operands and returns the resulting string.Spaces are always added between operands and a newline is appended.

  • Arguement
    返回一个字符串,不做任何输出

func ExampleSprintln() {
	const name, age = "Kim", 22
	s := fmt.Sprintln(name, "is", age, "years old.")

	io.WriteString(os.Stdout, s) // Ignoring error for simplicity.

	// Output:
	// Kim is 22 years old.
}
ExampleSprintln()
Kim is 22 years old.
总结

所有的函数名都是_print_格式的,其中_代表某个标志,标志及其含义如下:

标志含义
_f格式化
_ln换行
F_输入到某个对象
S_只返回而不做输出

且,
fmt.Fprintf和fmt.Printf是一对
fmt.Fprint和fmt.Print是一对
fmt.Fprintln和fmt.Println是一对
不带F前缀的函数均是对带F前缀函数的调用,其输入对象均设置为标准输出

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/945059.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号