GO

go test -v 会运行*_test.go

go test 会自动查找并运行以 TestXxx(t *testing.T) 命名的测试函数

go run hello.go 需要package mainfunc main()

package mapreduce 想运行

需要被package main import

或者命名为*_test.go

基本语法:

var x int = 10
x := 10

fruits := []string{"a", "b", "c"}
fruits = append(fruits, "d")
fmt.Println(fruits)


普通函数。
func add(a int, b int) int {
    return a + b
}

Go Struct没有类static函数。
type Person struct {
    Name string
    Age  int
    Add func(int, int) int //可以定义函数类型字段,但是需要实例化赋值才能使用。
}

绑定在Struct上的方法。
func (p Person) SayHello(name string) {
		fmt.Println("Hello " + name + ", I'm " + p.Name)
}

type Speaker interface {
		SayHello(name string) 
}

Me := Person{"std", 20}

Interface: Go处于Java和Python之间。

显式implement编译检验
JavaYY
GoNY
PythonNN

Go 并发基本:go func()则在新的goroutine 中异步执行,调用方不等待。

func main() {
    ch := make(chan string)
    go func() {
        ch <- "hello from goroutine"
    }()
	    msg := <-ch //会阻塞main,直到ch拿到消息。
    fmt.Println(msg)
}

i, _ := strconv.Atoi(s)
s := strconv.Itoa(123)

channel、lock(mutex)、cond(条件变量)就构成了 Go 并发的核心结构。

持有锁=能跑