Golang string Formatting

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Golang string Formatting

Package Mainimport "FMT" type point struct {x, y Int}func main () {//format integer, using '%d ' is a//standard way to output integer type in decimal//with a character Number decimal integer (int) (%LD,%ld: Long),%HD: Output short shape. ) FMT. Println ("=====%d, output decimal = =") fmt. Printf ("%d\n", "//output:110")//output integer binary representation of FMT. Println ("=====%b, output binary = = =") fmt. Printf ("%b\n", "//output:1101110")//output integer value corresponding to the character (char): one byte, 8 bits//can refer to ASCII FMT. Println ("=====%c, output a value character (char) = = =") fmt. Printf ("%c\n", "//output:a")//output a value of hexadecimal, each byte of the string output FMT with two characters. Println ("=====%x, output a value of hexadecimal, each string of bytes with two characters output = = =") fmt. Printf ("0x%x\n", "ten") fmt. Printf ("%x\n", "abc")//output:0xa//output:616263//Output floating-point value FMT. Println ("=====%f, output floating-point value = = =") fmt. Printf ("%f\n", 27.89)//output:27.890000//Output basic string FMT. Println ("=====%s, output basic string = = = =") fmt. Printf ("%s-%s-%s\n", "I", "AM", "Batu")//output:i-am-batu//outputs a string with double quotes FMT. Println ("=====%q, output string with double quotes = = =") fmt. Printf ("%q\n", "string")//output: "StrinG "//GO provides several print formats to format the general go value p: = Point{1, 2} fmt. Println ("=====%p, output a pointer value = = =") fmt. Printf ("%p\n", &p)//output:0xc042004390 FMT. Println ("=====%v, output struct object value = = =") fmt. Printf ("%v\n", p)//output: {1 2}//If the formatted value is a struct object, then the formatted output of '%+v ' is FMT. Println ("=====%+v, output struct member name and value = = =") fmt. Printf ("%+v\n", p)//output: {x:1 y:2} fmt. Println ("=====% #v, output a value of go syntax representation = = = =") fmt. Printf ("% #v \ n", p)//output:main.point{x:1, y:2} fmt. Println ("=====%t, output a value of data type = = = =") fmt.    Printf ("%t\n", p)//output:main.point//When outputting numbers, it is often necessary to control the width and accuracy of the output. You can use a number at the back of the% to control the width of the output, by default the output is right-aligned, and the left plus the space fmt. Println ("===== control output width and accuracy = = =") fmt.   Printf ("|%5d|%5d|\n", 345)//output: |  12|    345| Fmt. Println ("===== output width, specify floating point = = =") fmt. Printf ("|%5.2f|%5.2f|\n", 1.2, 3.456)//output: | 1.20|    3.46| Fmt. Println ("===== Left aligned = = = = =") fmt. Printf ("|%-5.2f|%-5.2f|\n", 1.2, 3.45)//output: |1.20 |3.45 |}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.