YouTip LogoYouTip

Go Fmt Sprintf

[![Image 1: Go Basic Syntax](#) Go Basic Syntax](#) Go can use fmt.Sprintf to format strings, as follows: fmt.Sprintf(Formatting Verb, Parameter List...) * **Formatting Verb:** In string form, formatting specifiers start with %, %s for string format, %d for decimal integer format. * **Parameter List:** Multiple parameters separated by commas, the number must match the number in the formatting verb, otherwise a runtime error will occur. ## Example package main import( "fmt" "io" "os" ) func main(){ // go: format string and assign to new string, use fmt.Sprintf // %s represents string var stockcode="000987" var enddate="2020-12-31" var url="Code=%s&endDate=%s" var target_url=fmt.Sprintf(url,stockcode,enddate) fmt.Println(target_url) // Another example, %d represents integer const name, age ="Kim",22 s := fmt.Sprintf("%s is %d years old.n", name, age) io.WriteString(os.Stdout, s)// For simplicity, ignore some errors } The output is: Code=000987&endDate=2020-12-31Kim is 22 years old. ## Example package main import( "fmt" "io" "os" ) func main(){ // Declare numeric variables const num1, num2, num3 =5,10,15 // Call Sprintf() function s := fmt.Sprintf("%d + %d = %d", num1, num2, num3) // Use WriteString() function to output the result to terminal // "os.Stdout" is the content of the string io.WriteString(os.Stdout, s) } The output is: 5 + 10 = 15 Go String Formatting Verbs: | Format | Description | | --- | --- | | %v | Output the original value | | %+v | On the basis of %v, expand the struct field names and values | | %#v | Output the value in Go syntax format | | %T | Output the type and value in Go syntax format | | %% | Output % itself | | %b | Integer displayed in binary | | %o | Integer displayed in octal | | %d | Integer displayed in decimal | | %x | Integer displayed in hexadecimal | | %X | Integer displayed in hexadecimal with uppercase letters | | %U | Unicode character | | %f | Floating point number | | %p | Pointer, displayed in hexadecimal | ## Example package main import"fmt" func main(){ name :="John" age :=30 height :=175.5 result := fmt.Sprintf("Name: %s, Age: %d, Height: %.2f", name, age, height) fmt.Println(result) } In the above example, in the format string "Name: %s, Age: %d, Height: %.2f", %s, %d and %.2f are replaced by the values of name, age and height variables respectively, where %.2f represents floating point format with two decimal places. The output is: Name: John, Age: 30, Height: 175.50 %.2f is a floating point formatting option, it is used to control the decimal precision of floating point numbers, where **2** represents the number of decimal places to keep, you can modify it as needed. More usage examples: ## Example package main import( "fmt" "os" ) type point struct{ x, y int } func main(){ p := point{1,2} fmt.Printf("%vn", p) fmt.Printf("%+vn", p) fmt.Printf("%#vn", p) fmt.Printf("%Tn", p) fmt.Printf("%tn",true) fmt.Printf("%dn",123) fmt.Printf("%bn",14) fmt.Printf("%cn",33) fmt.Printf("%xn",456) fmt.Printf("%fn",78.9) fmt.Printf("%en",123400000.0) fmt.Printf("%En",123400000.0) fmt.Printf("%sn",""string"") fmt.Printf("%qn",""string"") fmt.Printf("%xn","hex this") fmt.Printf("%pn",&p) fmt.Printf("|%6d|%6d|n",12,345) fmt.Printf("|%6.2f|%6.2f|n",1.2,3.45) fmt.Printf("|%-6.2f|%-6.2f|n",1.2,3.45) fmt.Printf("|%6s|%6s|n","foo","b") fmt.Printf("|%-6s|%-6s|n","foo","b") s := fmt.Sprintf("a %s","string") fmt.Println(s) fmt.Fprintf(os.Stderr,"an %sn","error") } The output is: {1 2}{x:1 y:2} main.point{x:1, y:2} main.point true1231110!1c878.9000001.234000e+081.234000E+08"string"""string""68657820746869730xc0000b4010| 12| 345|| 1.20| 3.45||1.20 |3.45 || foo| b||foo |b | a string an error ### Alignment By using width and alignment parameters in the format string, you can control the alignment of the generated string. Common alignment parameters are: * `%s`: String format, can use the following alignment parameters: * `%s`: Default alignment, left-aligned. * `%10s`: Right-aligned with width 10. * `%-10s`: Left-aligned with width 10. * `%d`: Integer format, can use the following alignment parameters: * `%d`: Default alignment, right-aligned. * `%10d`: Right-aligned with width 10. * `%-10d`: Left-aligned with width 10. * `%f`: Floating point format, can use the following alignment parameters: * `%f`: Default alignment, right-aligned. * `%10f`: Right-aligned with width
← Vue3 InstallJs Chrome β†’