- Reference: Go Chinese Docs, Bolg_1, Bolg_2 TDD
Fundamental Type
Go language contain Seven types of fundamental data structure: Boolean, Integer, Float, Complex Number, Character, String, Error
Type | Actual Type | Length(byte) | Default Value | Remark |
---|---|---|---|---|
Boolean | bool | 1 | false | |
Integer | int,uint | 4 or 8 | 0 | 32 bits or 64 bits |
Integer | int8, uint8 | 1 | 0 | -128 |
Integer | int16, uint16 | 2 | 0 | -32768 ~ 32767, 0 ~ 65535 |
Integer | int32, uint32 | 4 | 0 | -2.1 billion ~ 2.1 billion, 0 ~ 4.2 billion , rune is the alias of int32 |
Integer | int64, uint64 | 8 | 0 | |
Float | float32 | 4 | 0.0 | |
Float | float64 | 8 | 0.0 | |
Complex | complex64 | 8 | ||
Complex | complex128 | 16 | ||
Character | byte | 1 | 0 | byte is the same as uint8 |
Character | rune | 4 | 0 | Unicode Code Point, the same as int32 |
String | string | “” | UTF-8 string |
Features
Go language use value propagation as transfer method. That means Go use the copy the value to transfer data. The fundamental type is Unchangeable,if you modify fundamental type , Go will create new value and return this , the original var is not changed.
For Example:
1 | package main |
Fundamental type value is the copy value, so the original value never change actually, So that is the Thread-safe type.
Compound Data Structure
Go language contain seven types of compound data structure: Pointer, Array, Slice, Map, Channel, Struct, Interface .
Type | Actual Type | Default Value | Remark |
---|---|---|---|
Array | array | Value Type | |
Slice | slice | nil | Reference Type |
Map | map | nil | Reference Type |
Channel | channel | nil | Reference Type |
Struct | struct | Value Type | |
Interface | interface | nil | Reference Type |
Function | function | nil | Reference Type |
Reference Type
Reference is the opposite with fundamental type: Any modify operation will change the value of variable that reference it . Reference type variable contain the pointer to the underlying data structure. In go language, transfer value copy is also used in reference type, while is only copy the pointer part.
For Example:
1 | package main |
Struct Type
A struct is just a named collection of fields where you can store data. Declare a struct like this:
1 | type Person struct { |
The inner type in struct could be any type: Fundamental type or Reference type. In actual use stage, if the value would be changed, reference type is recommend, on the contrary fundamental type is better.
Transfer struct type in function is also value propagation, Go language allow us transfer struct value of struct pointer, they has the similar feature with fundamental type and reference type.
1 | package main |
1 | package main |
If the struct contain reference type, the reference type value will not be changed , if you pass the struct value .For Example:
1 | package main |
Interface Type
Interfaces are a very powerful concept in statically typed languages like Go because they allow you to make functions that can be used with different types and create highly-decoupled code whilst still maintaining type-safety.
1 | package main |
Self Define Type
type
is also used to define self fundamental type, we can use is as struct . Like define unique function.
1 | type Bitcoin int |