GO 中输出打印的常用函数

时间:2023-03-09 09:07:23
GO 中输出打印的常用函数

1.Println

可以打印字符串和变量(任何类型)    println函数在输出后自动增加一个换行

例: a:=10

b:=“string”

fmt.Println(a)  //right

fmt.Println(“abc”)  //right

fmt.Println(b)  //right

fmt.Println("this is %d",a)  //error Printin 不可以进行格式化输出,即不可以用%进行某个变量的格式化输出

fmt.Println(i, j, c, python, java) :只是输出括号中的变量对应的类型的值,如果放逗号就如左边

1 2 true false no!

2.Printf

只可以打印格式化的输出和输出字符串类型的变量,不可以输出整形变量!!

例:fmt.Printf("this is %d",a)  //right

fmt.Printf(b)  //right     字符型的变量Printf 可以输出

fmt.Println(a)  //error      整形不可以输出

3.Print

可以打印字符串形常量,不可以打印变量

例:fmt.Print("你好世界")   //right

输入:

输入函数

  • 第一种:fmt.Scanf("格式化字符串", 地址列表)
var num int
    //第一种:
    //fmt.Scanf("格式化字符串", 地址列表)
    fmt.Scanf("%d", &num)
    fmt.Println("num = ", num)

第二种: fmt.Scan(地址列表)

    // 第二种
    var num int
    // fmt.Scan(地址列表)
    // 注意: 不能用于指定格式化字符串
    fmt.Scan( &num)
    fmt.Println("num = ", num)

第三种: fmt.Scanln(地址列表)

    //第三种
    // fmt.Scanln(地址列表)
    var num int
    fmt.Scanln(&num)  // 输入 32 323
    fmt.Println("num = ", num)  // 输出 32

转载自:https://www.jianshu.com/p/ca4dba43dbfc