本文主要是介绍初学go语言println() vs fmt.Println,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
初学Go语言,会不会有如下疑问?
// test.go
package mainimport ("fmt""unsafe"
)const (Unknown = "abc_const"Female = len(Unknown)Male = unsafe.Sizeof(Female)
)// const (
// a = iota
// b
// c
// )const (a = iota //0b //1c //2d = "ha" //独立值,iota += 1e //"ha" iota += 1f = 100 //iota +=1g //100 iota +=1h = iota //7,恢复计数i //8
)func main() {const LENGTH int = 10const WIDTH int = 5var area int// const a, b, c = 1, false, "str" //多重赋值area = LENGTH * WIDTHfmt.Printf("面积为 : %d\n", area)println()println(Unknown, Female, Male)fmt.Println(a, b, c, d, e, f, g, h, i)
}
输出:
面积为 : 50
0 1 2 ha ha 100 100 7 8abc_const 9 8
此处为什么先fmt.Print函数簇执行,后println执行?
println vs fmt.println
//println: 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.//fmt.Println: 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.
println是用于打印标准输出,fmt.Println 是用于打印标准输出。
两个不同的描述符,把两个流都输出到终端,输出的先后顺序不确定。
解决办法:可以把流分开输出, 各看各的就没问题了
go run test.go 1>std_out.log 2>std_err.logcat std_err.logabc_const 9 8
cat std_out.log
面积为 : 50
0 1 2 ha ha 100 100 7 8
这篇关于初学go语言println() vs fmt.Println的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!