Golang 格式化

slug
golang-fmt
date
Mar 8, 2023
tags
技术&产品
summary
type
Post
status
Published
Last edited time
Mar 9, 2023 07:07 AM

Golang 输出格式化

Introduction

Go is a popular programming language that is widely used for its simplicity, efficiency, and ease of use. One of the key features of Go is its ability to format output in a variety of ways. In this blog post, we will discuss Golang's output formatting and how it can be used to create more readable and organized code.

Formatting Strings

In Go, the fmt package is used for input and output operations. One of the most common ways to format output in Go is by using the fmt.Printf function. The Printf function is used to format strings with placeholders. For example:
In this example, the %s and %d are placeholders for the string and integer values, respectively. When the code is executed, the placeholders are replaced with the corresponding values. The output will be:

Formatting Numbers

Go provides several options for formatting numbers. One of the most common ways is to use the fmt.Sprintf function. The Sprintf function is used to format strings with placeholders just like Printf, but instead of printing the output to the console, it returns a formatted string. For example:
In this example, the %.2f is a placeholder for the floating-point value, which is formatted to two decimal places. The output will be:

Formatting Dates and Times

Formatting dates and times is also straightforward in Go. The time package is used to work with dates and times in Go, and it provides a variety of options for formatting dates and times. For example:
In this example, the Format function is used to format the current time in the specified layout. The output will be:

Conclusion

In conclusion, Go provides a variety of options for formatting output, including strings, numbers, and dates and times. By using these formatting options, you can create more readable and organized code, making it easier to understand and maintain. The fmt package and the time package are powerful tools that can help you achieve this goal.

Customizing Output Formats

In addition to the standard formatting options provided by Go, you can also customize your own output formats. This is particularly useful when you need to output data in a specific format or when you want to create your own custom formatting strings.

Customizing Printf Formats

To customize the formatting string in Printf, you can use the following verbs:
  • %v formats the value in a default format.
  • %+v formats the value with additional information about the struct fields.
  • %#v formats the value in Go syntax.
  • %T formats the type of the value.
  • %t formats the Boolean value.
  • %b formats the integer as a binary number.
  • %c formats the integer as a Unicode character.
  • %d formats the integer as a decimal number.
  • %o formats the integer as an octal number.
  • %x formats the integer as a hexadecimal number.
  • %X formats the integer as a hexadecimal number with uppercase letters.
  • %e formats the floating-point number in scientific notation.
  • %E formats the floating-point number in scientific notation with uppercase letters.
  • %f formats the floating-point number in decimal notation.
  • %g formats the floating-point number in either decimal or scientific notation, depending on the precision.
  • %G formats the floating-point number in either decimal or scientific notation with uppercase letters, depending on the precision.
  • %s formats the string.
  • %p formats the pointer address.
For example:
In this example, the first Printf statement uses the %s and %d verbs to format the string and integer values, respectively. The second Printf statement uses the %v verb to format the values in their default format. The output will be:

Customizing Sprintf Formats

To customize the formatting string in Sprintf, you can use the same verbs as in Printf. For example:
In this example, the %.2f verb is used to format the floating-point value to two decimal places. The output will be:

Customizing Time Formats

To customize the time format in the time package, you can use the following verbs:
  • 2006 year
  • 01 month
  • 02 day
  • 15 hour
  • 04 minute
  • 05 second
  • Mon month name
  • Jan month abbreviation
  • Monday weekday name
  • Mon weekday abbreviation
  • pm or PM AM/PM
For example:
In this example, the format string is customized to display the month abbreviation, day, year, hour, minute, and second. The output will be:

Conclusion

In this blog post, we have discussed the various options for formatting output in Go, including strings, numbers, and dates and times. We have also shown how you can customize your own output formats using the fmt package and the time package. By using these formatting options, you can create more readable and organized code, making it easier to understand and maintain.
 
notion ai

© JimYan 2023 - 2024