func Print(a …interface{}) (n int, err error)
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.
使用其操作数的默认格式打印格式并写入标准输出。当两个操作数都不是字符串时,在两个操作数之间添加空格。它返回写入的字节数和遇到的任何写入错误。
▾ Example
package main
import (
"fmt"
)
func main() {
const name, age = "starkwang", 22
fmt.Print(name, " is ", age, " years old.\n")
// It is conventional not to worry about any
// error returned by Print.
}
starkwang is 22 years old.
func Printf
func 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.
根据格式说明符输出格式,并写入标准输出。它返回写入的字节数和遇到的任何写入错误。
▾ Example
package main
import (
"fmt"
)
func main() {
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.
}
func Println
func Println(a …interface{}) (n int, err error)
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.
使用其操作数的默认格式和写入标准输出的 Println 格式。总是在操作数之间添加空格,并在其后添加一个换行符。它返回写入的字节数和遇到的任何写入错误。
▾ Example
Something 例子
package main
import (
“fmt”
)
func main() {
const name, age = “starkwang”, 22
fmt.Println(name, “is”, age, “years old.”)
// It is conventional not to worry about any
// error returned by Println.
}
Print系列函数会将内容输出到系统的标准输出,区别在于Print函数直接输出内容,Printf函数支持格式化输出字符串,Println函数会在输出内容的结尾添加一个换行符。
func Print(a ...interface{}) (n int, err error)
func Printf(format string, a ...interface{}) (n int, err error)
func Println(a ...interface{}) (n int, err error)
举个简单的例子:
func main() {
fmt.Print("在终端打印该信息。")
name := "starkwang"
fmt.Printf("我是:%s\n", name)
fmt.Println("在终端打印单独一行显示")
}
执行上面的代码输出:
在终端打印该信息。我是:starkwang
在终端打印单独一行显示