用Google Go語言實現http共享
作者:Mike_Zhang
我們今天要介紹一下用Google Go語言實現http共享。盡管作者更青睞Python語言多一些,但還是寫了一些代碼供大家參考。
用Google Go語言實現http共享,這個程序我一直在用,感覺還是python的方式更加靈活:如不指定端口,則默認開啟8000……
可python不給力,慢不說,還只允許一個客戶端,果斷決定自己寫一個!
好了,不多廢話了,代碼如下:
- /*
- File : httpShare.go
- Author : Mike
- E-Mail : Mike_Zhang@live.com
- */
- package main
- import (
- "http"
- "os"
- "strings"
- )
- func shareDir(dirName string,port string,ch chan bool){
- h := http.FileServer(http.Dir(dirName))
- err := http.ListenAndServe(":"+port,h)
- if err != nil {
- println("ListenAndServe : ",err.String())
- ch <- false
- }
- }
- func main(){
- ch := make(chan bool)
- port := "8000" //Default port
- if len(os.Args)>1 {
- port = strings.Join(os.Args[1:2],"")
- }
- go shareDir(".",port,ch)
- println("Listening on port ",port,"...")
- bresult := <-ch
- if false == bresult {
- println("Listening on port ",port," failed")
- }
- }
運行效果如下:
1、正常情況下:
2、端口被占用時:
好,就這些來,希望對你有幫助。
原文鏈接:http://www.cnblogs.com/MikeZhang/archive/2012/03/13/httpShareGolang20120312.html
【編輯推薦】
責任編輯:彭凡
來源:
博客園