Home >Backend Development >Golang >Introducing golang's % usage and related knowledge

Introducing golang's % usage and related knowledge

PHPz
PHPzOriginal
2023-03-30 10:55:262122browse

In the Go language, we often use % to perform formatted output operations. The usage of % is very flexible. Let's introduce the usage of % in golang and related knowledge.

  1. Basic usage:

%The most basic form of usage is to use it to replace variables with the specified format. For example, the following code replaces i with %d, and %d indicates that the output variable i is a decimal integer.

i := 100 
fmt.Printf("i = %d\n", i) //输出 i = 100

You can also add some flags after the % symbol to change the output format. For example, %f represents a floating point number, %.2f represents a floating point number with two decimal places, and %s represents a string.

 pi := 3.1415926 
 fmt.Printf("pi = %.2f\n", pi) //输出 pi = 3.14
 str := "hello go" 
 fmt.Printf("str = %s\n", str) //输出 str = hello go
  1. Alignment and padding

The flag after % can modify the conversion rules to control the width and precision of the output.

For example, we can use d to set the output width to 10 characters (insufficient padding with spaces).

i := 100 
fmt.Printf("|%10d|\n", i) //输出 |       100|

Also, if we want to fill the output with insufficient width with 0, we can use 0d.

i := 100 
fmt.Printf("|%010d|\n", i) //输出 |0000000100|
  1. Format date and time

In golang, you can also use % to format date and time. For example:

t := time.Now() //获取当前时间
fmt.Printf("%04d-%02d-%02d %02d:%02d:%02d\n", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())  //输出:2022-04-05 11:42:34

The common format description of time formatting is as follows:

  • %Y 4-digit year
  • %m 2-digit month
  • %d 2-digit number of days
  • %H 2-digit number of hours
  • %M 2-digit number of minutes
  • %S 2-digit number of seconds
  1. Custom formatting

In Go language, you can also customize formatted output. The fmt package provides the function of formatting width and precision adjustment, but the formatted output operation cannot be customized. If you need to customize formatted output, you can use text/template or html/template.

type Person struct { 
    Name string 
    Age int 
} 

func (p *Person) String() string { 
    return fmt.Sprintf("%s is %d years old.", p.Name, p.Age) 
} 

func main() { 
    p := &Person{"Jack", 23} 
    fmt.Printf("%s\n", p) //输出 Jack is 23 years old.
}

The above code defines a Person structure type, and then implements the String() method for it, which returns the specified formatted output.

Regarding % usage, the above are several common application scenarios and techniques. There are also some more advanced and flexible operations that need to be explored and applied in daily development.

The above is the detailed content of Introducing golang's % usage and related knowledge. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn