Full Stack Go Programming (Golang) from Beginner to Advance

1. Introduction

1. what you will learn

2. Introduction


2. Golang In Windows

3. Golang In Linux

4. Variables

变量: 用变量名指向一段内存空间((堆)), 变量类型指定了变量的所指向的空间大小

1. Variables Concept

2. Var Keyword Declaration - When the Type is Removed

package main

import "fmt"

func main() {
    var variable1 = 100
    var variable2 = "Go programming Language"
    var variable3 = 114.514

    fmt.Printf("The Value of Variable 1: %d\n", variable1)
    fmt.Printf("The Type of Variable 1: %T\n", variable1)

    fmt.Printf("The Value of Variable 2: %s\n", variable2)
    fmt.Printf("The Type of Variable 2: %T\n", variable2)

    fmt.Printf("The Value of Variable 3: %f\n", variable3)
    fmt.Printf("The Type of Variable 3: %T\n", variable3)
}

3. Var Keyword Declaration - When the Expression is Removed

package main

import "fmt"

func main() {
    // 没给初始值会赋予默认值(零值)
    var variable1 int     // 0
    var variable2 string  //
    var variable3 float64 // 0.000000

    fmt.Printf("The Value of Variable 1: %d\n", variable1)
    fmt.Printf("The Value of Variable 2: %s\n", variable2)
    fmt.Printf("The Value of Variable 3: %f\n", variable3)
}

4. Var Keyword Declaration - Multiple Variables of the Same Type

package main

import "fmt"

func main() {
    //var variable1, variable2, variable3 = 2, 456, 67
    var variable1, variable2, variable3 int = 2, 456, 67

    fmt.Printf("The Value of Variable 1: %d\n", variable1)
    fmt.Printf("The Value of Variable 2: %d\n", variable2)
    fmt.Printf("The Value of Variable 3: %d\n", variable3)
}

5. Var Keyword Declaration - Multiple Variables of Different Types

package main

import "fmt"

func main() {
    var variable1, variable2, variable3 = 2, "Golang", 34.56

    fmt.Printf("The Value of Variable 1: %d\n", variable1)
    fmt.Printf("The Type of Variable 1: %T\n", variable1)

    fmt.Printf("The Value of Variable 2: %s\n", variable2)
    fmt.Printf("The Type of Variable 2: %T\n", variable2)

    fmt.Printf("The Value of Variable 3: %f\n", variable3)
    fmt.Printf("The Type of Variable 3: %T\n", variable3)
}

6. Var Keyword Declaration - Default Value

package main

import "fmt"

func main() {
    var num1 float32     // 0
    var num2 int32       // 0
    var firstname string //
    var married bool     // false

    fmt.Println(num1)
    fmt.Println(num2)
    fmt.Println(firstname)
    fmt.Println(married)
}

7. Var Keyword Declaration - Variables Declaration Block

package main

import "fmt"

func main() {
    var (
        firstname = "Kim"
        num1      = 300
        num2      = 20.65
        married   = true
    )

    fmt.Println(firstname)
    fmt.Println(num1)
    fmt.Println(num2)
    fmt.Println(married)
}

8. Short Variables - Declaration and Initialization

package main

import "fmt"

func main() {
    var1 := 100
    var2 := "go programming"
    var3 := 135.94

    fmt.Printf("The Value of Var 1: %d\n", var1)
    fmt.Printf("The Type of Var 1: %T\n", var1)

    fmt.Printf("The Value of Var 2: %s\n", var2)
    fmt.Printf("The Type of Var 2: %T\n", var2)

    fmt.Printf("The Value of Var 3: %f\n", var3)
    fmt.Printf("The Type of Var 3: %T\n", var3)
}

9. Short Variables - Multiple Variables of Same and Different Types

same type

package main

import "fmt"

func main() {
    var1, var2, var3 := 100, 200, 300

    fmt.Printf("The Value of Var 1 : %d\n", var1)
    fmt.Printf("The Type of Var 1 : %T\n", var1)

    fmt.Printf("The Value of Var 2 : %d\n", var2)
    fmt.Printf("The Type of Var 2 : %T\n", var2)

    fmt.Printf("The Value of Var 3 : %d\n", var3)
    fmt.Printf("The Type of Var 3 : %T\n", var3)
}

multiple type

package main

import "fmt"

func main() {

    //s variable declaration by declare multiple variables by differents type in single line
    var1, var2, var3 := 300, "GO Programming", 86.54

    fmt.Printf("The Value of Var 1 : %d\n", var1)
    fmt.Printf("The Type of Var 1 : %T\n", var1)

    fmt.Printf("The Value of Var 2 : %s\n", var2)
    fmt.Printf("The Type of Var 2 : %T\n", var2)

    fmt.Printf("The Value of Var 3 : %f\n", var3)
    fmt.Printf("The Type of Var 3 : %T\n", var3)
}

10. Scope of Variables

define variable

package main

import "fmt"

func main() {

    //declare local variables
    var variable1, variable2 int = 100, 200

    fmt.Printf("Variable1 Value is : %d\n", variable1)
    fmt.Printf("Variable2 Value is : %d ", variable2)
}

define global and local variable

package main

import "fmt"

var golobalVariable int = 100

func main() {
    //local variable scope starts

    var localVariable int = 200

    fmt.Printf("Global Variable is %d\n", golobalVariable)

    fmt.Printf("Local Variable : %d\n", localVariable)

    display()

    // local variable scope ends
}

func display() {

    fmt.Printf("The Value of Global variable is %d", golobalVariable)
}

define global and local same name variable

package main

import "fmt"

var variable1 int = 500

func main() {

    var variable1 int = 200

    fmt.Printf("Global Variable is %d\n", variable1) // 200

}

5. Data Types


  目录