基本类型
bool string int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr byte unint8 rune int32 float32 float64 complex64 complex128
不支持隐式转换、别名和原类型不能进行隐式转换
一、布尔
package main import "fmt" //布尔类型 func main() { var b1 bool var b2 bool b1 = true b2 = false fmt.Printf("%T ,%t \n", b1, b1) fmt.Printf("%T ,%t \n", b2, b2) }
bool ,true bool ,false
二、数字
package main import "fmt" func main() { //定义整形 var age int = 200 fmt.Printf("%T,%d \n", age, age) //定义浮点型 var money float64 = 56.236 fmt.Printf("%T,%.5f,\n", money, money) }
int,200 float64,56.23600,
三、字符串
package main import "fmt" func main() { //字符串定义 var str string str = "hello" fmt.Printf("%T,%s\n", str, str) //字符定义,字符是整形 v1 := 'a' v2 := "a" fmt.Printf("%T,%d \n", v1, v1) fmt.Printf("%T,%s \n", v2, v2) // 字符串链接 fmt.Println("abc" + "def") //字符串转义 fmt.Println("abc\",def") // 字符串长度 var str2 string = "abcdef" fmt.Println(len(str2)) }
string,hello int32,97 string,a abcdef abc",def 6
四、类型转换
package main import "fmt" func main() { num1 := 3 num2 := 6.0 fmt.Printf("%T,%d \n", num1, num1) fmt.Printf("%T,%.3f \n", num2, num2) //类型转换 num3 := num1 + int(num2) fmt.Printf("%T,%d \n", num3, num3) }
int,3 float64,6.000 int,9
package type_test import "testing" type MyInt int64 func TestImplicit(t *testing.T) { var a int32 = 1 var b int64 b = int64(a) t.Log(a, b) var c MyInt c = MyInt(b) t.Log(a, b, c) }
== RUN TestImplicit type_test.go:12: 1 1 type_test.go:15: 1 1 1 --- PASS: TestImplicit (0.00s)