您的位置 首页 golang

Go语言自学系列 | golang格式化输出

视频来源:B站《golang入门到项目实战 [2021最新Go语言教程,没有废话,纯干货!持续更新中…]》

一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!

附上汇总贴: Go语言自学系列 | golang格式化输出


下面实例使用到的 结构体

 type Website  struct  {
    Name string
}

// 定义结构体变量
var site = Website{Name: "duoke360"}
  

占位符

普通 占位符

 占位符                                                说明                                                举例                                                                                输出
%v                相应值的默认格式。                                                                 Printf ("%v", site),Printf("%+v", site)        {duoke360},{Name:duoke360}
                在打印结构体时,“加号”标记(%+v)会添加字段名
%#v                相应值的 go 语法表示                                                        Printf("#v", site)                                                main.Website{Name:"duoke360"}
%T                相应值的类型的Go语法表示                                                Printf("%T", site)                                                main.Website
%%                字面上的百分号,并非值的占位符                                        Printf("%%")                                                        %
  

布尔 占位符

 
  

整数占位符

 占位符                                                说明                                                举例                                                                        输出
%b                 二进制 表示                                                                        Printf("%b", 5)                                                101
%c                相应 Unicode 码点所表示的字符                                        Printf("%c", 0x4E2D)                                中
%d                十进制表示                                                                        Printf("%d", 0x12)                                        18
%o                八进制表示                                                                        Printf("%o", 10)                                        12
%q                单引号围绕的字符字面值,由Go语法安全地转义                Printf("%q", 0x4E2D)                                '中'
%x                十六进制表示,字母形式为小写 a-f                                Printf("%x", 13)                                        d
%X                十六进制表示,字母形式为大写 A-F                                Printf("%x", 13)                                        D
%U                Unicode格式:U+1234,等同于 "U+%04X"                        Printf("%U", 0x4E2D)                                U+4E2D
  

浮点数 和复数的组成部分(实部和虚部)

 占位符                                                说明                                                                                                举例                                                                        输出
%b                无小数部分的,指数为二的幂的科学计数法,与  strconv .FormatFloat        
                的 'b' 转换格式一致。例如 -123456p-78
%e                科学计数法,例如 -1234.456e+78                                                                        Printf("%e", 10.2)                                                        1.020000e+01
%E                科学计数法,例如 -1234.456E+78                                                                        Printf("%e", 10.2)                                                        1.020000E+01
%f                有小数点而无指数,例如 123.456                                                                        Printf("%f", 10.2)                                                        10.200000
%g                根据情况选择 %e 或 %f 以产生更紧凑的(无末尾的0)输出                                Printf("%g", 10.20)                                                        10.2
%G                根据情况选择 %E 或 %f 以产生更紧凑的(无末尾的0)输出                                Printf("%G", 10.20+2i)                                                (10.2+2i)
  

字符串 与字节切片

 占位符                                                说明                                                                                                举例                                                                        输出
%s                输出字符串表示(string类型或[] byte )                                                        Printf("%s", []byte("多课网"))                多课网
%q                双引号围绕的字符串,由Go语法安全地转义                                                        Printf("%q", "多课网")                                "多课网"
%x                十六进制, 小写字母 ,每字节两个字符                                                                Printf("%x", "golang")                                                676f6c616e67
%X                十六进制,大写字母,每字节两个字符                                                                Printf("%X", "golang")                                                676F6C616E67
  

指针

 占位符                                                说明                                                                                                举例                                                                        输出
%p                十六进制表示,前缀 0x                                                                                        Printf("%p", &site)                                                        0x4f57f0
  

实例

 package main

 import  (
    "fmt"
)

type WebSite struct {
    Name string
}

func main() {
    site := WebSite{Name: "duoke360"}
    fmt.Printf("site: %vn", site)
    fmt.Printf("site: %#vn", site)
    fmt.Printf("site: %Tn", site)
    a := 100
    fmt.Printf("a: %Tn", a)
    fmt.Println("%%")

    b := true
    fmt.Printf("b: %tn", b)

    i := 8
    fmt.Printf("i: %vn", i)
    fmt.Printf("i: %bn", i)
    i = 96
    fmt.Printf("i: %cn", i)
    fmt.Printf("i: %xn", 100)
    fmt.Printf("i: %xn", 1234)

    x := 100
    p := &x
    fmt.Printf("i: %pn", p)
}
  

运行结果

 [Running] go run "/Users/guoliang/Desktop/test.go"
site: {duoke360}
site: main.WebSite{Name:"duoke360"}
site: main.WebSite
a: int
%%
b: true
i: 8
i: 1000
i: `
i: 64
i: 4d2
i: 0xc00012a028
  

文章来源:智云一二三科技

文章标题:Go语言自学系列 | golang格式化输出

文章地址:https://www.zhihuclub.com/86557.shtml

关于作者: 智云科技

热门文章

评论已关闭

39条评论

  1. Unrestricted non-commercial use is permitted provided the original work is properly cited Of course it is up to man

  2. Nevertheless, most men would require more time for tadalafil to be optimally effective; in particular, patients with severe ED or older patients should be encouraged to optimize the period of sexual stimulation

  3. While this is not an FDA-approved use of the drug, the American Urological Association has included the use of sildenafil citrate in the treatment of PE in its Guideline on the Pharmacologic Management of Premature Ejaculation

  4. I went in for blood work 7 days after I got a positive OPK and my pogesterone level was 8.

  5. 370 A 55 response rate for palliation of pain and 71 of bleeding was reported although severe bowel complications occurred in six of 42 patients, probably related to the high dose per fraction employed PeledAW, FosterRD, GarwoodER, et al

  6. In the third level realm, the power has been greatly improved, so it Benefits Of Cbd Oil hempbomb cbd gummies 5ct can be released

  7. com 20 E2 AD 90 20Viagra 20Tallinna 20Resepti 20 20Quel 20Est 20Le 20Meilleur 20Viagra 20Naturel quel est le meilleur viagra naturel Italian insurer Unipol is likely to sell on themarket a 3

  8. Philips MF, Bagley LJ, Sinson GP, Raps EC, Galetta SL, Zager EL, et al In Vivo Regulation of CLA 1 SR BI Protein Expression

  9. Since that time, tremendous advances have been made in our understanding of the molecular mechanisms of ERО± activity and in the application of this knowledge to the development of therapies for the prevention and treatment of breast cancer

  10. TRAIL may also promote NF ОєB signaling 18 which can be both pro survival 19 and inflammatory, exemplified by induction of pro inflammatory cytokines and chemokines, such as interleukin IL 6, 8, and CXC motif ligand 1 CXCL1 in a NF ОєB 20, or a FADD and caspase 8 dependent manner 21, 22

  11. Extreme caution should be exercised when administering paclitaxel injection to such patients, with dose reduction as recommended in DOSAGE AND ADMINISTRATION, TABLE 17

  12. The blue lines indicate the likely pathway of the sciatic nerves hitting these when injecting is very painful and can also be very dangerous

  13. Department of Medicine, Saga University School of Medicine, Japan; and Cardiovascular Division of Medicine M After 25, 50, or 100mg Royal Jelly is given to infertile men, Royal jelly appeared to be ineffective at increasing sperm count increases were similar to control group but significantly better than control at increasing sperm motility; increasing motility by 25

  14. For example, some of a therapy that was very successful in reducing the tumor the anticancer drugs are given as a single dose or perhaps mass at the start of chemotherapy may become less effec several doses over a few days

  15. This was mirrored by the phospho mimicking mutants AQP2 S256A and AQP2 S256D, where AQP2 S256A containing endosomes tubulated, whereas AQP2 S256D containing endosomes did not In experimental studies, metformin, a biguanide agent used in type 2 diabetes mellitus, 257 has demonstrated favorable effects on the prostacyclin, endothelin and nitric oxide signaling pathways

  16. LINC00520 shRNA 2, antisense, 5 AATTCAAAAAACCTCAAATCTTTCGAGAACActcg agTGTTCTCGAAAGATTTGAGGT Molsidomine May enhance the hypotensive effect of Blood Pressure Lowering Agents

  17. B c if it is going to work it should in those 5 months It can take normal, healthy couples many months to achieve a pregnancy and often you may need to use the kit for 3 to 4 months before achieving pregnancy

  18. They didn t explain but I knew that it was time for them to remove Eve s ashes from the furnace and to grind down any remaining large fragments of bone Antiproliferative effects of Turkish pomegranate Punica granatum L

  19. I did a cycle in March and only took one cap per night unilateral military action against Syria

  20. A high corn oil diet strongly stimulates mammary carcinogenesis, while a high extra virgin olive oil diet has a weak effect, through changes in metabolism, immune system function and proliferation apoptosis pathways

  21. In the multicenter EARSS study in France, so far published only in abstract form 61, 798 patients with septic shock of less than 6 hours duration were randomized to receive 100 ml of 20 albumin or 100 ml of 0 anti- wrinkle- cream tamsulosin 400mcg od Some Western diplomats and analysts have already said itwill be difficult to verify whether Syria has given up all itschemical arms since there is no known reliable inventory of thestockpile or its whereabouts

  22. We know that exercise and healthy eating improves health after breast cancer, but this has been studied mostly in women who have finished their treatment I m so freaked pissed I decided to experiment for a few shows and destroy my physique last minute

  23. Here, we report a lineage tracing tool for MECs based on intraductal injection of lineage specific Cre expressing adenovirus Ad Cre

  24. The characteristics of patients from two cohorts stratified by renal function recovery were listed in Table 1

  25. Taste With Tori Glassy Brown Cookies Is there a role for combined cytotoxic and endocrine therapies

  26. Pooled results from prophylactic trials showed a significant reduction of gynecomastia in pca patients treated with prophylactic tamoxifen 20 mg daily odds ratio 0 The mice were divided into 4 groups and treated with vehicle control, FF 90 mg kg per day, IP, Ang II 1000 ng kg per minute, SC, and Ang II plus FF

网站地图