溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

golang verb的作用是什么

發(fā)布時間:2021-06-24 09:29:54 來源:億速云 閱讀:157 作者:chen 欄目:編程語言

這篇文章主要介紹“golang verb的作用是什么”,在日常操作中,相信很多人在golang verb的作用是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”golang verb的作用是什么”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

golang verb的作用是什么

Printf、Sprintf、Fprintf三個函數(shù)的format參數(shù)中,均可放置占位符verb,來對對應(yīng)數(shù)值進(jìn)行格式化字符串。

通用verb

%v    值的默認(rèn)格式表示

%+v    類似%v,但輸出結(jié)構(gòu)體時會添加字段名

%#v    值的Go語法表示

%T    值的類型的Go語法表示

%%    百分號
package main

import "fmt"

type Foo struct {
	Fo  int
	Bar string
}

func main() {
	foo := Foo{}
	fmt.Printf("%v \n", foo)
	fmt.Printf("%+v \n", foo)
	fmt.Printf("%#v \n", foo)
	fmt.Printf("%T \n", foo)
	fmt.Printf("%% \n")
}

輸出:

{0 } 
{Fo:0 Bar:} 
main.Foo{Fo:0, Bar:""} 
main.Foo 

Chan、Func、Map、Ptr、Slice、UnsafePointer

%p    內(nèi)存地址,表示為十六進(jìn)制,并加上前導(dǎo)的0x
package main

import "fmt"

type Foo struct {
	bar int
}

func main() {
	s := []string{}
	fmt.Printf("%p \n", s)

	m := map[string]string{}
	fmt.Printf("%p \n", m)

	c := make(chan int)
	fmt.Printf("%p \n", c)

	f := func() {}
	fmt.Printf("%p \n", f)

	i := 1
	p := &i
	fmt.Printf("%p \n", &i)
	fmt.Printf("%p \n", p)

	foo := Foo{}
	fmt.Printf("%p \n", foo)
}

0x1195ab8 
0xc000070180 
0xc00006e060 
0x109b260 
0xc000016078 
0xc000016078 
%!p(main.Foo={0})

fmtBool

%t    內(nèi)存地址,表示為十六進(jìn)制,并加上前導(dǎo)的0x
package main

import "fmt"

func main() {
	t := true
	fmt.Printf("%t \n", t)

	f := false
	fmt.Printf("%t \n", f)
}

true 
false

fmtFloat

b
g
G
x
X
f
F
e
E

fmtComplex

fmtInteger

fmtString

fmtBytes

到此,關(guān)于“golang verb的作用是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI