print

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

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. const name, age = "starkwang", 22
  7. fmt.Print(name, " is ", age, " years old.\n")
  8. // It is conventional not to worry about any
  9. // error returned by Print.
  10. }

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

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. const name, age = "Kim", 22
  7. fmt.Printf("%s is %d years old.\n", name, age)
  8. // It is conventional not to worry about any
  9. // error returned by Printf.
  10. }

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.”)

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

}

Print系列函数会将内容输出到系统的标准输出,区别在于Print函数直接输出内容,Printf函数支持格式化输出字符串,Println函数会在输出内容的结尾添加一个换行符。

  1. func Print(a ...interface{}) (n int, err error)
  2. func Printf(format string, a ...interface{}) (n int, err error)
  3. func Println(a ...interface{}) (n int, err error)

举个简单的例子:

  1. func main() {
  2. fmt.Print("在终端打印该信息。")
  3. name := "starkwang"
  4. fmt.Printf("我是:%s\n", name)
  5. fmt.Println("在终端打印单独一行显示")
  6. }

执行上面的代码输出:

  1. 在终端打印该信息。我是:starkwang
  2. 在终端打印单独一行显示