> For the complete documentation index, see [llms.txt](https://minilabmemo.gitbook.io/golang-memo/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://minilabmemo.gitbook.io/golang-memo/basic/pkgfmt-bao.md).

# \[pkg]fmt包

## fmt

完整文件：<https://pkg.go.dev/fmt>

### General

#### %v %+v %#v

> %v the value in a default format when printing structs, the plus flag (%+v) adds field names
>
> <mark style="background-color:yellow;">%v會簡單印出內容值，但%+v會印欄位名</mark>
>
> %#v a Go-syntax representation of the value
>
> <mark style="background-color:yellow;">會印結構名+欄位名與內容值</mark>

* 範例：

這邊可以看到前兩者欄位之間還會有空白

```go
package main

import "fmt"

type student struct {
	name string
	age  int
}

func main() {
	s := &student{"lili", 12}
	fmt.Printf("%%v的方式  = %v\n", s)  //&{lili 12}
	fmt.Printf("%%+v的方式 = %+v\n", s) //&{name:lili age:12}
	fmt.Printf("%%#v的方式 = %#v\n", s) //&main.student{name:"lili", age:12}

	s2 := student{"amy", 12}
	fmt.Printf("%%v的方式  = %v\n", s2)  //{amy 12}
	fmt.Printf("%%+v的方式 = %+v\n", s2) //{name:amy age:12}
	fmt.Printf("%%#v的方式 = %#v\n", s2) //main.student{name:"amy", age:12}
	
	
}
```

延伸

* \[关于fmt包Fprint系列方法的性能问题]\(<https://blog.csdn.net/FishGoddess/article/details/104809139>)
