0%

GO_Learning_Fundamental_Type

type is a keyword in Go language. In my practical project, Type is the most common keyword, while it’s usage could be vary. Here list five usage of keyword type.

Type Alias

Type alias , just as its name implies, user could infer original type with type alias. rune and byte is classical implement of type alias;

  • Syntax

    1
    type type_alias = original_type
  • Example

    1
    2
    3
    4
    5
    6
    7
    8
    9
    type bype = uint8
    type rune = int32

    func main() {
    var b byte
    var u uint8
    fmt.Printf("type of b:%T\n", b) // type of b:uint8
    fmt.Printf("type of u:%T\n", u) // type of u:uint8
    }

    Type Define

Type define allow user define self type with some intents. Compare to original type, self type define may has the follow advantage: 1. More Descriptive 2. Binding Specific Method: etc;

  • Syntax:

    1
    type define_name original_type
  • 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
    27
    28
    29
    package main

    import "fmt"
    // Compare int, BitCoin is more specific, show more intent.
    type BitCoin int
    // Customize the unique implementation
    func (b BitCoin) String() string {
    return fmt.Sprintf("%d BTC", b)
    }

    // multi way to init type bitcoin
    func main() {
    bitCoin1 := BitCoin(999)
    fmt.Printf("type of bitCoin1: %T\n", bitCoin1)
    fmt.Printf("self define print output %s\n", bitCoin1)
    // type of bitCoin1: main.BitCoin
    // self define print output 999 BTC

    var bitCoin2 BitCoin
    bitCoin2 = 100
    fmt.Printf("type of bitCoin2: %T\n", bitCoin2)
    // type of bitCoin2: main.BitCoin
    // self define print output 100 BTC

    var bitCoin3 BitCoin = 100
    fmt.Printf("type of bitcoin3: %T\n", bitCoin3)
    // type of bitcoin3: main.BitCoin
    // self define print output 100 BTC
    }

    Struct Define

    A struct is just a named collection of fields where you can store data. The detail feature will introduce in other MD file.

  • Syntax

    1
    2
    3
    4
    5
    type Struct_name struct{
    field1 type1
    field2 type2
    ...
    }
  • Example:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    // define struct
    type Rectangle struct {
    Width float64
    Height float64
    }

    // define method binding instance Rectangle
    func (r Rectangle) Area() float64 {
    return r.Height * r.Width
    }
    // I hope you'll agree that passing a Rectangle to a function conveys our intent more clearly, but there are more benefits of using structs that we will cover later.

    Interface Define

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.

  • Syntax

    1
    2
    3
    type interface_name interface {
    method return_value
    }
  • 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
    27
    28
    29
    30
    31
    32
    33
    package main

    import "math"

    type Shape interface {
    Area() float64
    }

    type Rectangle struct {
    Width float64
    Height float64
    }

    func (r Rectangle) Area() float64 {
    return r.Height * r.Width
    }

    type Circle struct {
    Radius float64
    }

    func (c Circle) Area() float64 {
    return c.Radius * c.Radius * math.Pi
    }

    type Triangle struct {
    Height float64
    Down float64
    }

    func (t Triangle) Area() float64 {
    return t.Height * t.Down / 2
    }

    Wait, what?

    This is quite different to interfaces in most other programming languages. Normally you have to write code to say My type Foo implements interface Bar.

    But in our case

    • Rectangle has a method called Area that returns a float64 so it satisfies the Shape interface
    • Circle has a method called Area that returns a float64 so it satisfies the Shape interface
    • string does not have such a method, so it doesn’t satisfy the interface
    • etc.

    In Go interface resolution is implicit. If the type you pass in matches what the interface is asking for, it will compile.

    Decoupling

    Notice how our helper does not need to concern itself with whether the shape is a Rectangle or a Circle or a Triangle. By declaring an interface, the helper is decoupled from the concrete types and only has the method it needs to do its job.

    This kind of approach of using interfaces to declare only what you need is very important in software design and will be covered in more detail in later sections.

Type Find

Type judge is a common usage in programming. i.(type)

  • Syntax

    1
    instance.(type)
  • 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
    27
    package main

    import "fmt"

    // 实现数据类型的判断
    func checkType(i interface{}) string {
    switch i.(type) {
    case string:
    return "string"
    case int:
    return "int"
    case float64:
    return "float64"
    default:
    return "unknown"
    }
    }

    func main() {
    // 验证
    v1 := "hello world"
    v2 := 33
    v3 := 45.2
    fmt.Printf("the type of v1 is %s\n", checkType(v1))
    fmt.Printf("the type of v2 is %s\n", checkType(v2))
    fmt.Printf("the type of v3 is %s\n", checkType(v3))
    }