1. Integer
main Package Penalty for // Import "fmt" // Import "unsafe" Import ( "fmt" "unsafe" ) // ==================== presentation golang integer type use ======================= FUNC main () { // int ----------------------- ------ ----- // uint unsigned // int is negative // int / uint see the operating system type, four 32-bit bytes, 64-bit is 8 bytes var = I int. 1 FMT. println ( "= I", I) // int8 ----------------------- ----------- // test int8 range of -128 to 127, // other the int16, int32, int64, and so on ... var = 127 J int8 fmt.Println ( "= J", J) // test range (0-255) uint8, the other uint16, uint32, uint64 analogy to UInt16 = 255 K var fmt.Println ( "= K", K) // ------------ int, uint, rune, byte ----------- Like // rune and Int32 // byte and uint8 equivalent, when storing the character selection byte var A = int 8900 fmt.Println ( "= A", A) var. 1 B uint = var C = 255 byte fmt.Println ( "= B", B, "= C", C) // --- --------- integer details ----------- var N1 = 100 // N1 is what type? // plastic default int type // here we introduce to how to view the data type of a variable // fmt.Printf () can be used to do formatted output. fmt.Printf ( "Type T n1 of% \ n-", n1) // how to view a variable in the program size and data type occupies bytes (use more) var Int64 N2 = 10 // unsafe.Sizeof (n1 ) is a function of packet unsafe, can return the number of bytes occupied by the variable n1 fmt.Printf ( "n2, the number of bytes occupied by the type of% T n2 is% d", n2, unsafe.Sizeof ( n2)) //% digital T type //% d }
2. Float
main Package Import ( "FMT" "the unsafe" ) // float32 float64 using FUNC main () { var. price float32 = 89.12 fmt.Println ( "=. price",. price) = 89.12. price // var num1 float32 = -0.00089 var float64 = -7809656.09 num2 fmt.Println ( "num1 =", num1, "num2 =", num2) // num2 num1 = -0.00089 + 06 = -7.80965609e // mantissa part may be lost, resulting in loss of precision // float = + exponent bits + sign bit mantissa // bit binary decimal turn when there will be an infinite loop, ending bit will result in a loss var num3 float32 = -123.0000901 var Num4 float64 = -123.0000901 fmt.Println ( "num3 =", num3, " num4 = ", num4) // num3 = -123.00009 num4 = -123.0000901 // golang floating point numbers have a fixed range and field length, from OS (operating system) affected // Golang default floating-type statement is float64 = 1.1 Num5 var fmt.Printf ( "data type is Num5 T% \ n-", Num5) float64 // // decimal form: As: 5.12 .512 (there must be a decimal point) Num6: = 5.12 Num7: = .123 = //> 0.123 fmt.Println ( "Num6 =", Num6, "Num7 =", Num7) // Num6 = 0.123 = 5.12 Num7 form // scientific notation num8: = 5.1234e2 // 2 5.1234 * 10 's? power num9: = 5.1234E2 // 2 power shift 5.1234 * 10 of + alt + down arrow? num10: // 2 = 5.1234E2 power of 0.051234 5.1234 / 10? fmt.Println ( "Num8 =" , Num8, "num9 =", num9, "num10 =", num10) // = 512.34 num9 Num8 = 512.34 = 0.051234 num10 }
3. Character
main Package Import ( "FMT" ) // character types used in the presentation golang FUNC main () { var byte C1 = 'A' var byte C2 = '0' characters 0 // // When we direct output byte value, that is, a code value corresponding to the output character // 'a' ==> fmt.Println ( "= C1", C1) C1 = 97 // fmt.Println ( "C2 =", C2) C2 = // 48 / / If we want an output corresponding to the character, the output format required fmt.Printf ( "C1 = C2 = C% C% \ n-", C1, C2) // C3 var byte = 'North' // overflow overflow var c3 int = 'North' // overflow overflow fmt.Printf ( "c3 =% c c3 corresponding to the code value =% d \ n", c3 , c3) // c3 = c3 corresponding to the code value = North 21271 // directly to a assigning a variable number, and then press output format% c, the digital outputs corresponding unicode character var c4 int = 22269 // 22269 - > ' country' 120 -> 'X' FMT.Printf ( "c4 =% c \ n", c4) // c4 = States @ character type of operation may be made, corresponds to an integer, is run in transport code valuePrintf("c4=%c\n", c4) // c4=国 var n1 = 10 + 'a' // 10 + 97 = 107 fmt.Println("n1=", n1) // n1= 107 }
3.1 Code Description
3.2 Character Details
3.3 String nature
4. Boolean
main Package Import ( "FMT" "the unsafe" ) // bool type used in presentation golang FUNC main () { var B = to false fmt.Println ( "= B", B) // Note // 1. bool type uses 1 byte storage is fmt.Println ( "b footprint =", unsafe.Sizeof (B)) // 2. BOOL type can only take true or to false }
5. String
package main import ( "fmt" ) // 演示golang中string类型使用 func main() { //string的基本使用 var address string = "北京长城 110 hello world!" fmt.Println(address) // 字符串一旦赋值了,字符串就不能修改了:在Go中字符串是不可变的 // var str = "hello" // str[0] = 'a' //这里就不能去修改str的内容,即go中的字符串是不可变的。 // 字符串的两种表示形式(1) 双引号, 会识别转义字符(2) 反引号 // 以字符串的原生形式输出,包括换行和特殊字符,可以实现防止攻击 str2 := "abc\nabc" fmt.Println(str2) //使用的反引号 ``,和Python的r一样 str3 := ` package main import ( "fmt" "unsafe" ) // 演示golang中bool类型使用 func main() { var b = false fmt.Println("b=", b) // 注意事项 //1. bool类型占用存储空间是1个字节 fmt.Println("b 的占用空间 =", unsafe.Sizeof(b) ) //2. bool类型只能取true或者false } ` fmt.Println(str3) // 字符串拼接方式 var str = "hello " + "world" str += " haha!" fmt.Println(str) // 当一个拼接的操作很长时,怎么办,可以分行写,但是注意,需要将+保留在上一行 // python中是\表示分行 str4 := "hello " + "world" + "hello " + "world" + "hello " + "world" + "hello " + "world" + "hello " + "world" + "hello " + "world" fmt.Println(str4) var a int // 0 var b float32 // 0 var c float64 // 0 var isMarried bool // false var name string // "" //这里的%v 表示按照变量的值输出 fmt.Printf("a=%d,b=%v,c=%v,isMarried=%v name=%v",a,b,c,isMarried, name) // a=0,b=0,c=0,isMarried=false name= }
6. 基本数据类型的默认值
7. 基本数据类型的转换
7.1 语法
package main import ( "fmt" ) // 演示golang中基本数据类型的转换 // 不管是高精度转低精度还是...都需要强制转换 func main() { var i int32 = 100 // 希望将 i => float var n1 float32 = float32(i) var n2 int8 = int8(i) var n3 int64 = int64(i) //低精度->高精度 fmt.Printf("i=%v n1=%v n2=%v n3=%v \n", i ,n1, n2, n3) // 被转换的是变量存储的数据(即值),变量本身的数据类型并没有变化 fmt.Printf("i type is %T\n", i) // int32 // 在转换中,比如将 int64 转成 int8 【-128---127】 ,编译时不会报错, // 只是转换的结果是按溢出处理,和我们希望的结果不一样 var num1 int64 = 999999 var num2 int8 = int8(num1) fmt.Println("num2=", num2) }
7.2 注意事项