fmt.Printf of Golang Foundation

Keywords: Programming encoding Go

background

When printing in go language, we often see different printing methods, such as println, fmt.Println and fmt.Printf

Differences and usage

Source code of fmt.Println

// 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.
func Println(a ...interface{}) (n int, err error) {
	return Fprintln(os.Stdout, a...)
}

The source code of fmt.Printf is as follows

// Printf formats according to a format specifier and writes to standard output.
// It returns the number of bytes written and any write error encountered.
func Printf(format string, a ...interface{}) (n int, err error) {
	return Fprintf(os.Stdout, format, a...)
}

The source code of the built-in println is as follows:

// The println built-in function formats its arguments in an
// implementation-specific way and writes the result to standard error.
// Spaces are always added between arguments and a newline is appended.
// Println is useful for bootstrapping and debugging; it is not guaranteed
// to stay in the language.
func println(args ...Type)

According to the official source code and notes, we can get the following conclusions:

  • fmt.Println: can print out strings, and variables.
  • fmt.Printf: only the formatted string can be printed. println will output as you input the format. Printf needs to format the output with the output format
  • Built in println: it belongs to output to standard error stream and print. It is not recommended to use it when writing programs. It can be used for debug ging

fmt.Printf is more practical and can format the data structure you want

package main
import "fmt"
import "os"
type point struct {
    x, y int
}
func main() {
    //Go provides a variety of printing methods for the format design of conventional go values. For example, an instance of the point structure is printed here.
    p := point{1, 2}
    fmt.Printf("%v\n", p) // {1 2}
    //If the value is a struct, the formatted output of% + v will include the field name of the struct.
    fmt.Printf("%+v\n", p) // {x:1 y:2}
    //%#The v form outputs the Go syntax representation of this value. For example, the run source snippet of the value.
    fmt.Printf("%#v\n", p) // main.point{x:1, y:2}
    //Type of value to print, use% T.
    fmt.Printf("%T\n", p) // main.point
    //Formatting Booleans is simple.
    fmt.Printf("%t\n", true)
    //There are many ways to format integer numbers. Use% d for standard decimal format.
    fmt.Printf("%d\n", 123)
    //This output is a binary representation.
    fmt.Printf("%b\n", 14)
    //This outputs the corresponding character of a given integer.
    fmt.Printf("%c\n", 33)
    //%x provides hexadecimal encoding.
    fmt.Printf("%x\n", 456)
    //There are also many formatting options for floating-point types. Use% f for the most basic decimal format.
    fmt.Printf("%f\n", 78.9)
    //%E and% e format floating-point types as (slightly different) scientific notation representations.
    fmt.Printf("%e\n", 123400000.0)
    fmt.Printf("%E\n", 123400000.0)
    //Use% s for basic string output.
    fmt.Printf("%s\n", "\"string\"")
    //Output with double quotes like in Go source code, using% q.
    fmt.Printf("%q\n", "\"string\"")
    //Like the integer number above, the% x output uses a base-16 encoded string, which is represented by two characters per byte.
    fmt.Printf("%x\n", "hex this")
    //To output the value of a pointer, use% p.
    fmt.Printf("%p\n", &p)
    //When outputting numbers, you will often want to control the width and precision of the output results. You can use numbers after% to control the output width. The default result is right justified and blanks are filled in.
    fmt.Printf("|%6d|%6d|\n", 12, 345)
    //You can also specify the output width of the floating-point type, and you can also specify the output precision through the syntax of width. Precision.
    fmt.Printf("|%6.2f|%6.2f|\n", 1.2, 3.45)
    //For best alignment, use the - flag.
    fmt.Printf("|%-6.2f|%-6.2f|\n", 1.2, 3.45)
    //You may also want to control the width of string output, especially to make sure that they are aligned when the class table is output. This is the basic right aligned width representation.
    fmt.Printf("|%6s|%6s|\n", "foo", "b")
    //To align left, as with numbers, use the - flag.
    fmt.Printf("|%-6s|%-6s|\n", "foo", "b")
    //So far, we've seen Printf, which outputs formatted strings through os.Stdout. Sprint f formats and returns a string without any output.
    s := fmt.Sprintf("a %s", "string")
    fmt.Println(s)
    //You can use Fprintf to format and output to io.Writers instead of os.Stdout.
    fmt.Fprintf(os.Stderr, "an %s\n", "error")
}

Reference resources

https://blog.csdn.net/zgh0711/article/details/78843361

https://segmentfault.com/a/1190000015302789

Posted by Byron on Wed, 08 Jan 2020 07:18:43 -0800