0%

GO_Learning_Fundamental_Data_Structure

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 -128127, 0255, byte is the alias of uint8
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package main

import "fmt"

func main() {
name := "Bob"
fmt.Println(modify(name))
fmt.Println(name)
}

func modify(name string) string {
name = name + name
return name
}
// BobBob
// Bob

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package main

import "fmt"

func main() {
ages := map[string]int{"Bob": 18}
fmt.Println(ages)
modifyMap(ages)
fmt.Println(ages)
}

func modifyMap(ageMap map[string]int) {
ageMap["Bob"] = 81
}
//map[Bob:18]
//map[Bob:81]

Struct Type

A struct is just a named collection of fields where you can store data. Declare a struct like this:

1
2
3
4
type Person struct {
age int
name string
}

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package main

import "fmt"

type Person struct {
name string
age int
}

func main() {
bob := Person{name: "Bob", age: 18}
fmt.Println(bob)
modify(bob)
fmt.Println(bob)
}

func modify(person Person) {
person.age = person.age + 10
}
//{Bob 18}
//{Bob 18}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package main

import "fmt"

type Person struct {
name string
age int
}

func main() {
bob := Person{name: "Bob", age: 18}
fmt.Println(bob)
modify(&bob)
fmt.Println(bob)
}

func modify(person *Person) {
person.age = person.age + 10
}
//{Bob 18}
//{Bob 28}

If the struct contain reference type, the reference type value will not be changed , if you pass the struct value .For Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import "fmt"

type Person struct {
name string
age int
friend []string
}

func main() {
bob := Person{name: "Bob", age: 18, friend: []string{"jack"}}
fmt.Println(bob)
fmt.Printf("%p \n", &bob.friend)
modify(bob)
fmt.Println(bob)
}

func modify(person Person) {
fmt.Printf("%p \n", &person.friend)
person.friend = []string{"black"}
}
\\ {Bob 18 [jack]}
\\ 0xc00008e168
\\ 0xc00008e1c8
\\ {Bob 18 [jack]}

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main

import "math"

// Shape is implemented by anything that can tell us its Area.
type Shape interface {
Area() float64
}

// Rectangle has the dimensions of a rectangle.
type Rectangle struct {
Width float64
Height float64
}

// Area returns the area of the rectangle.
func (r Rectangle) Area() float64 {
return r.Width * r.Height
}

// Perimeter returns the perimeter of a rectangle.
func Perimeter(rectangle Rectangle) float64 {
return 2 * (rectangle.Width + rectangle.Height)
}

// Circle represents a circle...
type Circle struct {
Radius float64
}

// Area returns the area of the circle.
func (c Circle) Area() float64 {
return math.Pi * c.Radius * c.Radius
}

// Triangle represents the dimensions of a triangle.
type Triangle struct {
Base float64
Height float64
}

// Area returns the area of the triangle.
func (c Triangle) Area() float64 {
return (c.Base * c.Height) * 0.5
}

Self Define Type

type is also used to define self fundamental type, we can use is as struct . Like define unique function.

1
2
3
4
5
type Bitcoin int

func (b Bitcoin) String() string {
return fmt.Sprintf("%d BTC", b)
}