Golang string formatted string formatting Golang

Golang string formatting

 

 

Go for string formatting provides a good support. Let us look at some examples of commonly formatted string.

Copy the code
main Package 
Import ( 
    "FMT" 
    "OS" 
) 
type struct Point { 
    X, Y int 
} 
FUNC main () { 

    // Go provides several print format for the general format Go value, e.g. 
    // following% v printed value of the object structure a point 
    P: = {point. 1, 2} 
    fmt.Printf ( "% v \ n-", P) 

    // if the value is a formatted structure object, `% + v` formatted output 
    // will include member names and values structure 
    fmt.Printf ( "V +% \ n-", P) 

    @ #% `v` formatted output the output value indicates a syntax Go the way. 
    fmt.Printf ( "# V% \ n-", P) 

    // output using `% T` a data type value 
    fmt.Printf ("% T \ n-", P) 

    // Format Boolean variable 
    fmt. printf ( "% t \ n" ,


    // In this manner the output binary integer representation 
    fmt.Printf ( "% B \ n-", 14) 

    // Print out the integer value where the corresponding character 
    fmt.Printf ( "% c \ n" , 33 ) 

    // output using `% x` a value of hexadecimal representation 
    fmt.Printf ("% X \ n-", 456) 

    // float value format there are several methods. The most basic one is F`% ` 
    fmt.Printf (" F% \ n-", 78.9) 

    // properties E 'and`% `% E` Use scientific notation to output integer 
    fmt.Printf ("% e \ n-", 123,400,000.0) 
    fmt.Printf ("% E \ n-", 123,400,000.0) 

    // output using`% s` basic string 
    fmt.Printf ( "% S \ n-", "\" string \ "") 

    / character string / image output Go source as in double quotes, use OJ% ` 
    fmt.Printf ("% Q \ n-"," \ "string \" ") 

    // hexadecimal` x`% output string, each string with a two byte character output 
    fmt.Printf ( "% x \ n" , "hex this"


    // When the output of the digital time, often need to control the width and the accuracy of the output. 
    // width may be used a number located behind% to control the output of the default 
    case // the output is right-justified, plus space left 
    fmt.Printf ( "|% 6d |% 6d | \ n", 12, 345 ) 

    // you can also specify the output width of the float, and you can also specify a float 
    // output accuracy 
    fmt.Printf ( "|% 6.2f |% 6.2f | \ n-", 1.2, 3.45) 

    // the to the justify-left, `-` the use in Flag. 
    fmt.Printf (" |% -6.2f |% -6.2f | \ n-", 1.2, 3.45) 

    // you can specify the output width of the string to ensure that the output thereof alignment. Default 
    under //, the output is right-justified 
    fmt.Printf ( "|% 6S |% 6S | \ the n-", "foo", "b") 

    // To use the left-aligned before you can add width `- `No. 
    fmt.Printf (" |% -6S |% -6S | \ the n-"," foo "," b ") 

    //` Printf` output function is output to the command line `os.Stdout` you 
    / / `Sprintf` may be formatted string to the assignment to a variable 
    s: = fmt.

    // you can also use the output value to `Fprintf` formatted to io.Writers`` 
    fmt.Fprintf (os.Stderr, "AN% S \ n-", "error") 
}
Copy the code
Copy the code
{1 2}
{x:1 y:2}
main.point{x:1, y:2}
main.point
true
123
1110
!
1c8
78.900000
1.234000e+08
1.234000E+08
"string"
"\"string\""
6865782074686973
0xc042052070
|    12|   345|
|  1.20|  3.45|
|1.20  |3.45  |
|   foo|     b|
|foo   |b     |
a string
an error

 

Go for string formatting provides a good support. Let us look at some examples of commonly formatted string.

Copy the code
main Package 
Import ( 
    "FMT" 
    "OS" 
) 
type struct Point { 
    X, Y int 
} 
FUNC main () { 

    // Go provides several print format for the general format Go value, e.g. 
    // following% v printed value of the object structure a point 
    P: = {point. 1, 2} 
    fmt.Printf ( "% v \ n-", P) 

    // if the value is a formatted structure object, `% + v` formatted output 
    // will include member names and values structure 
    fmt.Printf ( "V +% \ n-", P) 

    @ #% `v` formatted output the output value indicates a syntax Go the way. 
    fmt.Printf ( "# V% \ n-", P) 

    // output using `% T` a data type value 
    fmt.Printf ("% T \ n-", P) 

    // Format Boolean variable 
    fmt. printf ( "% t \ n" ,


    // In this manner the output binary integer representation 
    fmt.Printf ( "% B \ n-", 14) 

    // Print out the integer value where the corresponding character 
    fmt.Printf ( "% c \ n" , 33 ) 

    // output using `% x` a value of hexadecimal representation 
    fmt.Printf ("% X \ n-", 456) 

    // float value format there are several methods. The most basic one is F`% ` 
    fmt.Printf (" F% \ n-", 78.9) 

    // properties E 'and`% `% E` Use scientific notation to output integer 
    fmt.Printf ("% e \ n-", 123,400,000.0) 
    fmt.Printf ("% E \ n-", 123,400,000.0) 

    // output using`% s` basic string 
    fmt.Printf ( "% S \ n-", "\" string \ "") 

    / character string / image output Go source as in double quotes, use OJ% ` 
    fmt.Printf ("% Q \ n-"," \ "string \" ") 

    // hexadecimal` x`% output string, each string with a two byte character output 
    fmt.Printf ( "% x \ n" , "hex this"


    // When the output of the digital time, often need to control the width and the accuracy of the output. 
    // width may be used a number located behind% to control the output of the default 
    case // the output is right-justified, plus space left 
    fmt.Printf ( "|% 6d |% 6d | \ n", 12, 345 ) 

    // you can also specify the output width of the float, and you can also specify a float 
    // output accuracy 
    fmt.Printf ( "|% 6.2f |% 6.2f | \ n-", 1.2, 3.45) 

    // the to the justify-left, `-` the use in Flag. 
    fmt.Printf (" |% -6.2f |% -6.2f | \ n-", 1.2, 3.45) 

    // you can specify the output width of the string to ensure that the output thereof alignment. Default 
    under //, the output is right-justified 
    fmt.Printf ( "|% 6S |% 6S | \ the n-", "foo", "b") 

    // To use the left-aligned before you can add width `- `No. 
    fmt.Printf (" |% -6S |% -6S | \ the n-"," foo "," b ") 

    //` Printf` output function is output to the command line `os.Stdout` you 
    / / `Sprintf` may be formatted string to the assignment to a variable 
    s: = fmt.

    // you can also use the output value to `Fprintf` formatted to io.Writers`` 
    fmt.Fprintf (os.Stderr, "AN% S \ n-", "error") 
}
Copy the code
Copy the code
{1 2}
{x:1 y:2}
main.point{x:1, y:2}
main.point
true
123
1110
!
1c8
78.900000
1.234000e+08
1.234000E+08
"string"
"\"string\""
6865782074686973
0xc042052070
|    12|   345|
|  1.20|  3.45|
|1.20  |3.45  |
|   foo|     b|
|foo   |b     |
a string
an error

Guess you like

Origin www.cnblogs.com/lgj8/p/12085288.html