Go HelloWorld 网络版和并发版

网络版

复制代码
package main

import (
    "net/http"
    "fmt"
)

func main() {
    http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
        fmt.Fprintf(writer, "<h1>Hello World %s!</h1>", request.FormValue("name"))
    })

    http.ListenAndServe(":8888", nil)
}
复制代码

并发版

复制代码
package main

import (
    "fmt"
    "time"
)

func printHelloWorld(i int, ch chan string) {
    ch <- fmt.Sprintf("Hellow World from goroutine %d!\n", i)
}

func main() {
    ch := make(chan string)
    for i:=0; i<5000; i++ {
        // go starts a go routine
        go printHelloWorld(i, ch)
    }

    for {
        msg := <- ch
        fmt.Println(msg)
    }
    time.Sleep(time.Millisecond)
}
复制代码

 

posted @   Vincen_shen  阅读(202)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示