golang几种输出函数的对比

golang中 Print Sprint Fprint的对比

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main

import (
"fmt"
"os"
)

type point struct {
x, y int
}

func main() {
str := "print"
fmt.Printf("%s\n", str) // "string" use a format
fmt.Print(str) // "string" not add a newline
fmt.Println(str, 1, 2) // "string 1 2" add a newline

str = "sprint"
sprintStr := fmt.Sprintf("%s\n", str) // "sprint" use a foamat
fmt.Print(sprintStr)

sprintStr = fmt.Sprint(str) // not add a new line
fmt.Print(sprintStr)

sprintStr = fmt.Sprintln(str) // add a new line
fmt.Print(sprintStr)

str = "fprint"
fmt.Fprintf(os.Stdout, "%s\n", str)
fmt.Fprint(os.Stdout, str)
fmt.Fprintln(os.Stdout, str)
}
Author: Rick
Link: https://rcrick.github.io/2019/08/24/golang几种输出函数的对比/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
  • 支付寶