欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Go語言中println和fmt.Println區(qū)別

 更新時(shí)間:2023年07月04日 15:09:49   作者:PigeonEssence  
本文主要介紹了Go語言中println和fmt.Println區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

作為一個(gè)剛剛接觸Go的萌新,在學(xué)習(xí)Go語言的時(shí)候發(fā)現(xiàn)demo用了兩種用法輸出語句:

package main
import "fmt"
func main() {
   const LENGTH int = 10
   const WIDTH int = 5  
   var area int
   const a, b, c = 1, false, "str" //多重賦值
   area = LENGTH * WIDTH
   fmt.Printf("面積為 : %d", area)
   println()
   println(a, b, c)  
}

其中運(yùn)用了println()和fmt包中的Printf()函數(shù)用于輸出,然后我又想起,再fmt函數(shù)中,還有一個(gè)相似的函數(shù)是

fmt.Println

于是我就研究了一下println()和fmt.Println()的區(qū)別與用法:

println()函數(shù):

我們點(diǎn)進(jìn)函數(shù)可以清楚地看到注釋:

// The println built-in function formats its arguments in an
// implementation-specific way and writes the result to standard error.
// Spaces are always added between arguments and a newline is appended.
// Println is useful for bootstrapping and debugging; it is not guaranteed
// to stay in the language.
func println(args ...Type)

簡單翻譯一下:

1.The println built-in function formats its arguments in an implementation-specific way and writes the result to standard error.
   println內(nèi)置函數(shù)以特定于實(shí)現(xiàn)的方式格式化其參數(shù),并將結(jié)果寫入標(biāo)準(zhǔn)錯(cuò)誤。
2.Spaces are always added between arguments and a newline is appended.
   始終在參數(shù)之間添加空格,并追加換行符。
3.Println is useful for bootstrapping and debugging; it is not guaranteed to stay in the language.
   Println用于引導(dǎo)和調(diào)試;但是不保證在未來的Go版本中繼續(xù)存在

fmt.println()函數(shù):        

// Println formats using the default formats for its operands and writes to standard output.
// Spaces are always added between operands and a newline is appended.
// It returns the number of bytes written and any write error encountered.
func Println(a ...interface{}) (n int, err error) {
	return Fprintln(os.Stdout, a...)
}

簡單翻譯一下:

1.Println formats using the default formats for its operands and writes to standard output.
   Println格式使用其操作數(shù)的默認(rèn)格式,并寫入標(biāo)準(zhǔn)輸出。
2.Spaces are always added between operands and a newline is appended.
   始終在操作數(shù)之間添加空格,并追加換行符。
3.It returns the number of bytes written and any write error encountered.
   它返回寫入的字節(jié)數(shù)和遇到的任何寫入錯(cuò)誤。

根據(jù)官方注釋,我們可以很清楚的看到在注釋中他們很大的區(qū)別是在于,fmt.println()是在fmt包下的方法,將結(jié)果寫入標(biāo)準(zhǔn)輸出,而println是在builtin包下的方法,將結(jié)果寫入標(biāo)準(zhǔn)錯(cuò)誤。

總結(jié)區(qū)別:

1.包不同:

fmt包:fmt 包使用函數(shù)實(shí)現(xiàn) I/O 格式化(類似于 C 的 printf 和 scanf 的函數(shù)), 格式化參數(shù)源自C,但更簡單。

builtin包:builtin包是go的預(yù)聲明定義,包括go語言中常用的各種類型和方法聲明,包括變量和常量兩部分.其詳細(xì)聲明在builtin.go文件中。

因?yàn)閎uiltin包是預(yù)申明的包,所以不需要import就可以使用,而 fmt 包需要提前import。

2.輸出方式不同:

輸入標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤也是很明顯的輸出體現(xiàn),使用菜鳥教程const iota的例子說明:

//iota
	const (
		a = iota   //0
		b          //1
		c          //2
		d = "ha"   //獨(dú)立值,iota += 1
		e          //"ha"   iota += 1
		f = 100    //iota +=1
		g          //100  iota +=1
		h = iota   //7,恢復(fù)計(jì)數(shù)
		i          //8
	)
	fmt.Println(a,b,c,d,e,f,g,h,i)
	println(a,b,c,d,e,f,g,h,i)

輸出:

很明顯可以看出,在Windows Goland IDE下:標(biāo)準(zhǔn)輸出是白色,錯(cuò)誤輸出是紅色。所以一般適用于debug的時(shí)候,并且不保證在未來的Go版本中繼續(xù)存在。一般輸出還是調(diào)用fmt包輸出更為妥當(dāng)。

3.方法返回值不同:

我們首先觀察兩個(gè)方法的代碼:

package fmt
func Println(a ...interface{}) (n int, err error) {
	return Fprintln(os.Stdout, a...)
}
package builtin
func println(args ...Type)

可以發(fā)現(xiàn),fmt.Println是有返回值參數(shù)的。

fmt.println():

  • 第一個(gè)返回值是:the number of bytes written,也就是往輸出上寫入了多少個(gè)字節(jié)
  • 第二個(gè)返回值是:any write error encountered,也就是寫入的任何錯(cuò)誤

也就是說fmt.println()可以實(shí)現(xiàn)println()中無法實(shí)現(xiàn)的字節(jié)數(shù)統(tǒng)計(jì)和錯(cuò)誤分析。

4.內(nèi)置print/println函數(shù)的調(diào)用不能接受數(shù)組和結(jié)構(gòu)體參數(shù)

//定義數(shù)組
	arr := [5]int{1,2,3,4,5}
	fmt.Println(arr)

成功輸出:

//定義數(shù)組
	arr := [5]int{1,2,3,4,5}
	println(arr)

輸出錯(cuò)誤:

 5.對于組合類型的參數(shù),內(nèi)置的print/println函數(shù)將輸出參數(shù)的底層值部的地址,而fmtlog標(biāo)準(zhǔn)庫包中的打印函數(shù)將輸出接口參數(shù)的動態(tài)值的字面形式。

func main() {
	var numbers = make([]int,3,5)
	printSlice(numbers)
}
func printSlice(x []int){
	fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
	println("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}

輸出結(jié)果:

 6.如果一個(gè)實(shí)參有String() stringError() string方法,那么fmtlog標(biāo)準(zhǔn)庫包里的打印函數(shù)在打印參數(shù)時(shí)會調(diào)用這兩個(gè)方法,而內(nèi)置的print/println函數(shù)則會忽略參數(shù)的這些方法。

func main() {
	conent, err := openFile()
	if err != nil {
		fmt.Printf("fmt.Printf:存在錯(cuò)誤,%v\n", err)
	} else {
		fmt.Println(string(conent))
	}
	if err != nil {
		println("println:存在錯(cuò)誤,%v\n",err)
	} else {
		println(string(conent))
	}
}
//只是模擬一個(gè)錯(cuò)誤
func openFile() ([]byte, error) {
	return nil, &fileError{}
}
type fileError struct {
}
func (fe *fileError) Error() string {
	return "文件錯(cuò)誤"
}

輸出結(jié)果:

到此這篇關(guān)于Go語言中println和fmt.Println區(qū)別的文章就介紹到這了,更多相關(guān)Go語言 println fmt.Println內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論