2.5. Go fmt.Sprintf format string

发布时间 : 2025-10-25 13:32:57 UTC      

Page Views: 9 views

Go can be used fmt.Sprintf to format the string in the following format:

fmt.Sprintf(Format style, parameter list…) 
  • Format style: in the form of a string, formatting symbols to % at the beginning %s string format, %d decimal integer format.

  • Parameter list: multiple parameters are separated by commas, and the number must correspond to the number in the formatting style one by one, otherwise the runtime will report an error.

2.5.1. Example #

package main import ( "fmt" "io" "os" ) func main() { // Format the string in go and assign it to a new string, using fmt.Sprintf // %S represents a 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 instance,% d represents an 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 as follows:

Code=000987&endDate=2020-12-31 Kim is 22 years old. 

Go string formatting symbol:

Format

Description

%v

Output by the original value of the value

%+v

Expand the structure field name and value on the basis of%v

%#v

Output values in Go language syntax format

%T

Output the types and values of the Go language syntax format

%%

Output% ontology

%b

The integer is displayed in binary mode

%o

Integers are displayed in octal mode

%d

Integers are displayed in decimal mode

%x

The integer is displayed in hexadecimal mode

%X

Integers are displayed in hexadecimal, uppercase letters

%U

Unicode character

%f

Floating point number

%p

Pointer, displayed in hexadecimal mode

2.5.2. Example #

package main import ( "fmt" "os" ) type point struct { x, y int } func main() { p := point{1, 2} fmt.Printf("%v\\n", p) fmt.Printf("%+v\\n", p) fmt.Printf("%#v\\n", p) fmt.Printf("%T\\n", p) fmt.Printf("%t\\n", true) fmt.Printf("%d\\n", 123) fmt.Printf("%b\\n", 14) fmt.Printf("%c\\n", 33) fmt.Printf("%x\\n", 456) fmt.Printf("%f\\n", 78.9) fmt.Printf("%e\\n", 123400000.0) fmt.Printf("%E\\n", 123400000.0) fmt.Printf("%s\\n", "\\"string\\"") fmt.Printf("%q\\n", "\\"string\\"") fmt.Printf("%x\\n", "hex this") fmt.Printf("%p\\n", &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 %s\\n", "error") } 

The output is as follows:

{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 0xc0000b4010 | 12| 345| | 1.20| 3.45| |1.20 |3.45 | | foo| b| |foo |b | a string an error 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。