第7课:Go语言基础实例

前面的例子可能还是不够的,在这一节,会列出各种"能直接运行的例子"
跟着本节例子自己写一遍,运行一遍,Go语言就基本掌握了

Go语言作为一个现代化的语言,其优势在对高并发的支持,下面例子可以很好的理解并发并行
package main

import (
	"fmt"
	"runtime"
)

var quit chan int = make(chan int)

func loop(id int) {
	for i := 0; i < 10; i++ {
		fmt.Printf("%d ", id)
	}
	quit <- 0
}

func main() {
	runtime.GOMAXPROCS(2)

	for i := 0; i < 3; i++ {
		go loop(i)
	}

	for i := 0; i < 3; i++ {
		<-quit
	}
}
看看运行结果,找找规律,理解并发与并行

除去高大上的功能,基本概念也是要掌握的,下面是一些基础例子
1.
package main

import "fmt"//注意这里一定是双引号,单引号报错

func main() {//工程里只能有一个main,如果多个文件里都有main会报错

    fmt.Println("godeye" + "org")
    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)
}

运行结果:
golang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
false

2.
package main

import "fmt"
import "math"

const s string = "constant"

func main() {
    fmt.Println(s)

    const n = 500000000
    const d = 3e20 / n
    fmt.Println(d)	
    fmt.Println(int64(d))
    fmt.Println(math.Sin(n))
}

运行结果:
constant
6e+11
600000000000
-0.28470407323754404

3.
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 {
        fmt.Println("www.godeye.org")
        break
    }
}

运行结果:
1
2
3
7
8
9
loop

4.
package main

import "fmt"

func main() {

    var a [5]int
    fmt.Println("emp:", a)
    a[4] = 100
    fmt.Println("set:", a)
    fmt.Println("get:", a[4])
    fmt.Println("len:", len(a))
    b := [5]int{1, 2, 3, 4, 5}
    fmt.Println("dcl:", b)

    var twoD [2][3]int
    for i := 0; i < 2; i++ {
        for j := 0; j < 3; j++ {
            twoD[i][j] = i + j
        }
    }
    fmt.Println("2d: ", twoD)
}

运行结果:
emp: [0 0 0 0 0]
set: [0 0 0 0 100]
get: 100
len: 5
dcl: [1 2 3 4 5]
2d:  [[0 1 2] [1 2 3]]

5.
package main

import "fmt"

func main() {
    m := make(map[string]int)
    m["k1"] = 7
    m["k2"] = 13

    fmt.Println("map:", m)

    v1 := m["k1"]
    fmt.Println("v1: ", v1)
    fmt.Println("len:", len(m))

    delete(m, "k2")
    fmt.Println("map:", m)

    _, prs := m["k2"]
    fmt.Println("prs:", prs)

    n := map[string]int{"foo": 1, "bar": 2}
    fmt.Println("map:", n)
}

运行结果:
map: map[k1:7 k2:13]
v1:  7
len: 2
map: map[k1:7]
prs: false
map: map[foo:1 bar:2]

6.
package main

import "fmt"

func plus(a int, b int) int {
    return a + b
}	

func plusPlus(a, b, c int) int {
    return a + b + c
}	

func main() {	

    res := plus(1, 2)
    fmt.Println("1+2 =", res)

    res = plusPlus(1, 2, 3)
    fmt.Println("1+2+3 =", res)
}

运行结果:
1+2 = 3
1+2+3 = 6

7.
package main

import "fmt"

type person struct {
    name string
    age  int
}

func main() {

    fmt.Println(person{"Bob", 20})
    fmt.Println(person{name: "Alice", age: 30})
    fmt.Println(person{name: "Fred"})
    fmt.Println(&person{name: "Ann", age: 40})

    s := person{name: "Sean", age: 50}
    fmt.Println(s.name)

    sp := &s
    fmt.Println(sp.age)

    sp.age = 51
    fmt.Println(sp.age)
}

运行结果:
{Bob 20}
{Alice 30}
{Fred 0}
&{Ann 40}
Sean
50
51

8.
package main

import "fmt"

func f(from string) {
    for i := 0; i < 3; i++ {
        fmt.Println(from, ":", i)
    }
}

func main() {

    f("direct")

    go f("goroutine")

    go func(msg string) {
        fmt.Println(msg)
    }("going")

    var input string
    fmt.Scanln(&input)
    fmt.Println("done")
}

运行结果:
direct : 0
direct : 1
direct : 2
goroutine : 0
going
goroutine : 1
goroutine : 2
<enter>
done

9.
package main

import "fmt"

func main() {

    messages := make(chan string)

    go func() { messages <- "ping" }()

    msg := <-messages
    fmt.Println(msg)
}

运行结果:
ping