首页 > 编程知识 正文

go语言底层是c语言吗,go语言数据类型

时间:2023-05-04 00:42:33 阅读:47047 作者:3659

目录

println ) )函数:

fmt.println ()函数:

总结差异:

1 .包不一样:

2 .输出方式不同:

3 .方法返回值不同:

4 .内置的print/println函数调用不能接受数组和结构参数。

5 .对于组合类型参数,内置的print/println函数输出参数底部的地址,而fmt和log标准库包中的打印函数输出接口参数动态值的文字。

6 .如果string (字符串或Error ) string方法实际参与,则fmt和log标准库包中的打印函数在打印参数时都会调用这两者

方法,而内置的print/println函数则会忽略参数的这些方法。


作为一个刚刚接触Go的萌新,在学习Go语言的时候发现demo用了两种用法输出语句:

        原文是菜鸟教程中的DemoGo 语言常量 | 菜鸟教程:

package mainimport "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) }

其中运用了println()和fmt包中的Printf()函数用于输出,然后我又想起,再fmt函数中,还有一个相似的函数是

fmt.Println

于是我就研究了一下println()和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.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内置函数以特定于实现的方式格式化其参数,并将结果写入标准错误。2.Spaces are always added between arguments and a newline is appended. 始终在参数之间添加空格,并追加换行符。3.Println is useful for bootstrapping and debugging; it is not guaranteed to stay in the language. Println用于引导和调试;但是不保证在未来的Go版本中继续存在 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.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格式使用其操作数的默认格式,并写入标准输出。2.Spaces are always added between operands and a newline is appended. 始终在操作数之间添加空格,并追加换行符。3.It returns the number of bytes written and any write error encountered. 它返回写入的字节数和遇到的任何写入错误。

        根据官方注释,我们可以很清楚的看到在注释中他们很大的区别是在于,fmt.println()是在fmt包下的方法,将结果写入标准输出,而println是在builtin包下的方法,将结果写入标准错误。

总结区别: 1.包不同:

fmt包:

        fmt 包使用函数实现 I/O 格式化(类似于 C 的 printf 和 scanf 的函数), 格式化参数源自C,但更简单。

builtin包:

        builtin包是go的预声明定义,包括go语言中常用的各种类型和方法声明,包括变量和常量两部分.其详细声明在builtin.go文件中。

        因为builtin包是预申明的包,所以不需要import就可以使用,而 fmt 包需要提前import。

2.输出方式不同:

        输入标准输出和标准错误也是很明显的输出体现,使用菜鸟教程const iota的例子说明:

//iotaconst (a = iota //0b //1c //2d = "ha" //独立值,iota += 1e //"ha" iota += 1f = 100 //iota +=1g //100 iota +=1h = iota //7,恢复计数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下:标准输出是白色,错误输出是红色。所以一般适用于debug的时候,并且不保证在未来的Go版本中继续存在。一般输出还是调用fmt包输出更为妥当。

3.方法返回值不同:

我们首先观察两个方法的代码:

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

可以发现,fmt.Println是有返回值参数的。

fmt.println():

             第一个返回值是:the number of bytes written,也就是往输出上写入了多少个字节

             第二个返回值是:any write error encountered,也就是写入的任何错误

也就是说fmt.println()可以实现println()中无法实现的字节数统计和错误分析。

4.内置print/println函数的调用不能接受数组和结构体参数。 //定义数组arr := [5]int{1,2,3,4,5}fmt.Println(arr)

成功输出:

 

//定义数组arr := [5]int{1,2,3,4,5}println(arr)

输出错误:

 

 5.对于组合类型的参数,内置的print/println函数将输出参数的底层值部的地址,而fmt和log标准库包中的打印函数将输出接口参数的动态值的字面形式。

        

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

输出结果:

 6.如果一个实参有String() string或Error() string方法,那么fmt和log标准库包里的打印函数在打印参数时会调用这两个方法,而内置的print/println函数则会忽略参数的这些方法。 func main() {conent, err := openFile()if err != nil {fmt.Printf("fmt.Printf:存在错误,%vn", err)} else {fmt.Println(string(conent))}if err != nil {println("println:存在错误,%vn",err)} else {println(string(conent))}}//只是模拟一个错误func openFile() ([]byte, error) {return nil, &fileError{}}type fileError struct {}func (fe *fileError) Error() string {return "文件错误"}

输出结果:

 以上就是对于println和fmt.Println的区别总结,还有别的问题,欢迎补充

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。