Go基础编程:格式化输出、类型转换、类型别名
生活随笔
收集整理的這篇文章主要介紹了
Go基础编程:格式化输出、类型转换、类型别名
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用fmt包來格式化字符串
fmt.Printf()格式字符串:
? ? ? ? ? ? ? ? ? ? ? ??
?
? ? ? ? ? ? ? ? ? ? ? ?
//整型a := 15fmt.Printf("a = %b\n", a) //a = 1111fmt.Printf("%%\n") //只輸出一個%//字符ch := 'a'fmt.Printf("ch = %c, %c\n", ch, 97) //a, a//浮點型f := 3.14fmt.Printf("f = %f, %g\n", f, f) //f = 3.140000, 3.14fmt.Printf("f type = %T\n", f) //f type = float64//復數類型v := complex(3.2, 12)fmt.Printf("v = %f, %g\n", v, v) //v = (3.200000+12.000000i), (3.2+12i)fmt.Printf("v type = %T\n", v) //v type = complex128//布爾類型fmt.Printf("%t, %t\n", true, false) //true, false//字符串str := "hello go"fmt.Printf("str = %s\n", str) //str = hello go?
類型轉換
Go語言中不允許隱式轉換,所有類型轉換必須顯式聲明,而且轉換只能發生在兩種相互兼容的類型之間。
var ch byte = 97//var a int = ch //err, cannot use ch (type byte) as type int in assignmentvar a int = int(ch)類型別名
type bigint int64 //int64類型改名為bigintvar x bigint = 100type (myint int //int改名為myintmystr string //string改名為mystr)?
?
?
?
總結
以上是生活随笔為你收集整理的Go基础编程:格式化输出、类型转换、类型别名的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Go基础编程:基础数据类型
- 下一篇: 编程之美读书笔记2.1—求二进制数中1的