3.Basic Data Types
# Integer
There are two major groups of integer:
- signed:
- int8
- int16
- int32
- int64
- unsigned
- uint8
- uint16
- uint32
- uint64
Type | Description |
---|---|
uint8 | unsinged 8 byte (0 ~ 255) |
uint16 | unsinged 16 byte (0 ~ 65535) |
uint32 | unsigned 32 byte (0 ~ 4294967295) |
uint64 | unsigned 64 byte (0 ~ 18446744073709551615) |
int8 | signed 8 byte (-128 ~ 127) |
int16 | signed 16 byte (-32768 ~ 32767) |
int32 | signed 32 byte (-2147483648 ~ 2147483647) |
int64 | signed 64 byte (-9223372036854775808 ~ 9223372036854775807) |
# special integer
Type | Description |
---|---|
uint | in 32-byte system is uint32, in 64-byte system is uint64 |
int | in 32-byte system is int32, in 64-byte system is int64 |
uintptr | unsigned integer, use for saving a pointer |
# Octal & Hexadecimal
in Go
you can not define binary integer directly
package main
import "fmt"
func main(){
var a int = 10
fmt.Printf("%d \n", a) // 10
fmt.Printf("%b \n", a) // 1010 binary
var b int = 077
}
2
3
4
5
6
7
8
9
10
11
12
# Float
In Go
has float32
and float64
. According to the IEEE 754
standard: the range of float32
is 3.4e38
math.MaxFloat32
. float64
has range 1.8e308
math.MaxFloat64
.
use the following code to print:
fm.Printf("%f\n", math.pi)
fm.Printf("%.2f\n", math.pi)
2
by default the float is float64
f1 := 1.23456
fmt.Printf("%T", f1) // by default the decimal is float64
2
# Complex
complex64
and complex128
var c1 complex64
c1 = 1 + 2i
var c2 complex128
c2 = 2 + 3i
fmt.Println(c1)
fmt.Println(c2)
2
3
4
5
6
# Boolean
Go
use bool
to determine Boolean data type only has true
and false
two values
Note:
- The default value of
bool
isfalse
Go
not allowed transfer other data type intobool
- can not use
fool
for numerical operations
# String
String is a basic data type in the go
language, use UTF-8
format. value is the content inside of the double quotes(")
, you can use no ASCII character directly, such as:
s1 := "Hello"
s2 := "你好"
2
character use single quote(')
, such as:
c1 := 'h'
c2 := '你'
// 1 byte = 8 bit
// 1 char 'A' = 1 byte
// 1 utf-8 letter '你' = 3 byte
2
3
4
5
6
7
# Escape code
Escape code | Meaning |
---|---|
\r | Carriage return |
\n | Newline |
\t | Tabs |
\' | Single quote |
\" | Double quotes |
\\ | Backslash |
# Multiline string
In go
if you want to determine a multiline string you need to use Backticks
, such as:
str := `First line
Second line
Third line
`
fmt.Println(str)
2
3
4
5
Line breaks between backticks will be regarded as line breaks in the string, but all escape characters are invalid, and the text will be output as it is.
# Common methods of strings
Method | Description |
---|---|
len(str) | get the length of the string |
+ or fmt.Sprintf() | Concatenated string |
strings.Split() | Split string |
strings.contains() | check if string contain certain string |
strings.HasPrefix() , strings.HasSuffix() | check if has Pre\Suffix |
strings.Index() , strings.LastIndex() | where the substring appears |
strings.Join(a[] string, sep string) | Join strings into a string |