The basic data types Chapter V of the Go language
Integer
Types of | description |
---|---|
uint8 | Unsigned 8-bit integers (0 to 255) |
uint16 | Unsigned 16-bit integers (0 to 65535) |
uint32 | Unsigned 32-bit integers (0 to 4,294,967,295) |
uint64 | 64-bit unsigned integer (0-18446744073709551615) |
int8 | Signed 8-bit integer (-128 to 127) |
int16 | Signed 16-bit integer (-32768 to 32767) |
int32 | Signed 32-bit integer (-2147483648 to 2147483647) |
int64 | Signed 64-bit integer (-9223372036854775808 to 9223372036854775807) |
uint8
Is, we know thebyte
type,int16
the corresponding C languageshort
type,int64
corresponding to the C languagelong
type
Special Integer
Types of | description |
---|---|
uint | The operating system is a 32-bit uint32 , 64-bit operating system isuint64 |
int | The operating system is a 32-bit int32 , 64-bit operating system isint64 |
uintptr | Unsigned int, for storing a pointer |
Digital literal syntax
package main import "fmt" func main(){ // 十进制 var a int = 10 fmt.Printf("%d \n", a) // 10 fmt.Printf("%b \n", a) // 1010 占位符%b表示二进制 // 八进制 以0开头 var b int = 077 fmt.Printf("%o \n", b) // 77 // 十六进制 以0x开头 var c int = 0xff fmt.Printf("%x \n", c) // ff fmt.Printf("%X \n", c) // FF }
Float
Go language supports two floating-point number:
float32
andfloat64
package main import ( "fmt" "math" ) func main() { fmt.Println(math.MaxFloat32) fmt.Println(math.MaxFloat64) } >>> 3.4028234663852886e+38 1.7976931348623157e+308
plural
complex64 and complex128
var c1 complex64 c1 = 1 + 2i var c2 complex128 c2 = 2 + 3i fmt.Println(c1) fmt.Println(c2)
Complex64 real and imaginary part is 32 bits, complex128 the real and imaginary part 64.
Boolean value
- Boolean data only
true(真)
andfalse(假)
two values.
Remarks:
- Boolean variables default value is
false
. - Go language is not allowed in the integer cast to Boolean.
- That is not in python 0 means that False, 1 for True
- Boolean operations can not participate in value can not be converted to other types.
String
string
String value
双引号 ""
content or backtickss1 := "hello" s2 := "你好"
String escaping
Escaped with a backslash: \ r \ n, etc.
// 比如打印一个Windows平台下的一个文件路径 package main import ( "fmt" ) func main() { fmt.Println("str := \"c:\\Code\\lesson1\\go.exe\"") }
Multi-line strings
Multi-line strings, you must use anti-quote character
s1 := `第一行 第二行 第三行 \n \t ` fmt.Println(s1)
Common string operation
method | Introduction |
---|---|
len (str) | Seek length |
+ Or fmt.Sprintf | String concatenation |
strings.Split | Split |
strings.contains | Determine whether to include |
strings.HasPrefix,strings.HasSuffix | Prefix / Suffix Analyzing |
strings.Index(),strings.LastIndex() | Position of the substring occurring |
strings.Join(a[]string, sep string) | join operations, using the symbol splicing |
byte and rune type
The composition of each string element called "characters" can be obtained by traversing a character string or a single acquisition element. Character ( ') wrapped in single quotes
var a := '中' var b := 'x'
Go language character of the following two:
- byte type represents
ASCII码
a character.- byte type is actually a
uint8
type
- byte type is actually a
rune
Type, representing aUTF-8字符
rune
It is actually a typeint32
.
- byte type represents
func main() { s := "hello沙河" for i := 0; i < len(s); i++ { //byte fmt.Printf("%c", s[i]) } fmt.Println() for _, r := range s { //rune fmt.Printf("%c", r) } } >>> helloæ²æ²³ hello沙河
Modify the string
To modify the string need to first convert it into
[]rune
or[]byte
, after the completion of the conversion intostring
. Either conversion will re-allocate memory and copy the byte array.func main() { s1 := "big" // 强制类型转换 byteS1 := []byte(s1) byteS1[0] = 'p' fmt.Println(string(byteS1)) s2 := "白萝卜" runeS2 := []rune(s2) runeS2[0] = '红' fmt.Println(string(runeS2)) } >>> pig 红萝卜
Type Conversion
Go language only casts, no implicit type conversion. This syntax is used only when the mutual conversion between the two types of support.
func sqrtDemo() { var a, b = 3, 4 var c int // math.Sqrt()接收的参数是float64类型,需要强制转换 c = int(math.Sqrt(float64(a*a + b*b))) fmt.Println(c) } // 将a*a + b*b的结果强制转换成float64类型