当前位置 博文首页 > Go语言入门教程之基础语法快速入门

    Go语言入门教程之基础语法快速入门

    作者:admin 时间:2021-02-09 09:30

    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

    js
    下一篇:没有了