Go語言入門教程之基礎文法快速入門_Golang

來源:互聯網
上載者:User

Go語言是一個開源的,為建立簡單的,快速的,可靠的軟體而設計的語言。

Go語言實(示)例教程,通過過執行個體加註釋的方式來介紹Go語言的用法。

Hello World

第一個程式會輸出"hello world"訊息。原始碼如下:

複製代碼 代碼如下:

package main

import "fmt"

func main() {
    fmt.Println("hello world")
}

//通過go run來運行Go程式
$ go run hello-world.go
hello world
//它會花一段時間將原始碼編繹成二進位檔案,可以通過go build實現這一過程。
$ go build hello-world.go
$ ls
hello-world hello-world.go
//然後直接運行二進位檔案
$ ./hello-world
hello world

Values:實值型別

Go有許多實值型別包括:strings, integers, floats, booleans, 等。下面是一些基礎的樣本。

複製代碼 代碼如下:

package main

import "fmt"

func main() {

//字串可以使用 + 來串連
    fmt.Println("go" + "lang")

//整型和浮點數
    fmt.Println("1+1 =", 1+1)
    fmt.Println("7.0/3.0 =", 7.0/3.0)

    //布爾類型,你可以使用布林運算子
    fmt.Println(true && false)
    fmt.Println(true || false)
    fmt.Println(!true)
}

$ go run values.go
golang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
false

Variables:變數

在Go語言中,變數是顯式聲明的;編輯器可以在函數調用檢查類型的正確性。

複製代碼 代碼如下:

package main

import "fmt"

func main() {
    //用var聲明一個或多個變數
    var a string = "initial"
    fmt.Println(a)

    //你可以聲明多個變數
    var b, c int = 1, 2
    fmt.Println(b, c)

    //GO會通過預設值來確定變數類型
    var d = true
    fmt.Println(d)

    //有類型但沒有值的變數會被賦zero值zero-valued. int類型的零值是0.
    var e int
    fmt.Println(e)

    //:=語言是聲明和初始設定變數的簡寫, 例如
    //與:var f string = "short" 等價
    f := "short"
    fmt.Println(f)
}

$ go run variables.go
initial
1 2
true
0
short

Constants:常量

Go支援的常量有character, string, boolean, 和numeric 類型.

複製代碼 代碼如下:

package main

import "fmt"
import "math"

//常量用const聲明
const s string = "constant"

func main() {
    fmt.Println(s)

    //const聲明可以像var一樣在任何地方使用
    const n = 500000000

    //常量可以表示任意精度
    const d = 3e20 / n
    fmt.Println(d)

    //數字常量沒有類型直到被賦與,如通過下面的顯示指定
    fmt.Println(int64(d))

    //數字可以在上下文指定需要的類型,例如math.Sin需要一個float64的類型
    fmt.Println(math.Sin(n))
}

$ go run constant.go
constant
6e+11
600000000000
-0.28470407323754404

For:迴圈

for是Go語言中唯一的迴圈結構。下面是基本的三類迴圈類型。

複製代碼 代碼如下:

package main

import "fmt"

func main() {

//最基本的類型,一個單一的迴圈
    i := 1
    for i <= 3 {
        fmt.Println(i)
        i = i + 1
    }

    //一個精典的類型:帶初始化/條件的迴圈
    for j := 7; j <= 9; j++ {
        fmt.Println(j)
    }

    //通過條件的for迴圈會一直執行,直到你中斷或在閉包中返回。
    for {
        fmt.Println("loop")
        break
    }
}

$ go run for.go
1
2
3
7
8
9
loop


在range文法中我們還會看到另外一種for的用法。

If/Else:條件陳述式

Go通過if和else來實現條件分支

複製代碼 代碼如下:

package main

import "fmt"

func main() {
//一個基本用法
    if 7%2 == 0 {
        fmt.Println("7 is even")
    } else {
        fmt.Println("7 is odd")
    }

    //只有if的情況
    if 8%4 == 0 {
        fmt.Println("8 is divisible by 4")
    }

    //你可以在條件中聲明變數;任何聲明可以在所有的條件程式碼片段中使用
    if num := 9; num < 0 {
        fmt.Println(num, "is negative")
    } else if num < 10 {
        fmt.Println(num, "has 1 digit")
    } else {
        fmt.Println(num, "has multiple digits")
    }
}


Go語言中沒有三目(ternary if)運算子,你需要用if來實現
複製代碼 代碼如下:

$ go run if-else.go
7 is odd
8 is divisible by 4
9 has 1 digit

Switch:條件枚舉

Switch文法可以實現多種條件分支。

複製代碼 代碼如下:

package main

import "fmt"
import "time"

func main() {
//這裡是一個基礎的Switch
    i := 2
    fmt.Print("write ", i, " as ")
    switch i {
    case 1:
        fmt.Println("one")
    case 2:
        fmt.Println("two")
    case 3:
        fmt.Println("three")
    }

    //你可以使用多種條件匹配,同樣你可以使用預設匹配
    switch time.Now().Weekday() {
    case time.Saturday, time.Sunday:
        fmt.Println("it's the weekend")
    default:
        fmt.Println("it's a weekday")
    }

    //無條件的switch可以實現if/else類型的效果
    t := time.Now()
    switch {
    case t.Hour() < 12:
        fmt.Println("it's before noon")
    default:
        fmt.Println("it's after noon")
    }
}

$ go run switch.go
write 2 as two
it's the weekend
it's before noon

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.