Go 实用 网络服务开发

HTTP Get

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main

import (
"net/http"
"io/ioutil"
"fmt"
)

func main() {
resp, _ := http.Get("http://baidu.com")
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
resp.Body.Close()
}

goroutine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package main

import (
"fmt"
"time"
)

func fun(str string) {
for i := 0; i < 3; i++ {
fmt.Println(str, i)
time.Sleep(time.Microsecond)
}
}

func main() {
go fun("a: ")
go fun("b: ")
time.Sleep(time.Second)
fmt.Println("c: ")
}

##