现在的位置: 首页 > 综合 > 正文

Golang实现Http共享

2013年12月06日 ⁄ 综合 ⁄ 共 763字 ⁄ 字号 评论关闭

package main
import (

        
"net/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.Error())

                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"
)

        }

}

转载自:http://www.cnblogs.com/MikeZhang/archive/2012/03/13/httpShareGolang20120312.html

抱歉!评论已关闭.