自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用Google Go語言實現http共享

開發(fā) 開發(fā)工具
我們今天要介紹一下用Google Go語言實現http共享。盡管作者更青睞Python語言多一些,但還是寫了一些代碼供大家參考。

用Google Go語言實現http共享,這個程序我一直在用,感覺還是python的方式更加靈活:如不指定端口,則默認開啟8000……

可python不給力,慢不說,還只允許一個客戶端,果斷決定自己寫一個!

好了,不多廢話了,代碼如下:

  1. /*  
  2. File : httpShare.go  
  3. Author : Mike  
  4. E-Mail : Mike_Zhang@live.com  
  5. */  
  6. package main  
  7.  
  8. import (  
  9. "http"  
  10. "os"  
  11. "strings"  
  12. )  
  13.  
  14. func shareDir(dirName string,port string,ch chan bool){  
  15. :http.FileServer(http.Dir(dirName))  
  16. err :http.ListenAndServe(":"+port,h)  
  17. if err != nil {  
  18. println("ListenAndServe : ",err.String())  
  19. ch <- false  
  20. }  
  21. }  
  22.  
  23. func main(){  
  24. ch :make(chan bool)  
  25. port :"8000" //Default port   
  26. if len(os.Args)>1 {   
  27. port = strings.Join(os.Args[1:2],"")  
  28. }  
  29. go shareDir(".",port,ch)  
  30. println("Listening on port ",port,"...")  
  31. bresult :<-ch 
  32. if false == bresult {  
  33. println("Listening on port ",port," failed")  
  34. }  

運行效果如下:

1、正常情況下:

2、端口被占用時:

好,就這些來,希望對你有幫助。

原文鏈接:http://www.cnblogs.com/MikeZhang/archive/2012/03/13/httpShareGolang20120312.html

【編輯推薦】

  1. Google Go語言發(fā)布兩周年 不斷改進中
  2. Google Go:新興語言的代表
  3. 1月編程榜發(fā)布:Google Go意外奪得年度編程語言
  4. Google Go有啥用?以及何謂好的系統(tǒng)編程語言
  5. Google Go語言的快樂編程因素
責任編輯:彭凡 來源: 博客園
相關推薦

2012-08-06 08:50:05

Go語言

2023-05-08 07:55:05

快速排序Go 語言

2020-08-12 08:56:30

代碼凱撒密碼函數

2022-11-01 18:29:25

Go語言排序算法

2024-08-29 13:23:04

WindowsGo語言

2012-07-06 13:16:37

Google Go

2022-05-19 14:14:26

go語言限流算法

2022-02-21 18:16:38

Go語言枚舉

2021-07-12 15:50:55

Go 語言netstat命令

2024-06-06 09:47:56

2013-12-12 10:55:21

2023-07-31 08:01:13

二叉搜索測試

2021-07-26 09:47:38

Go語言C++

2022-04-18 10:01:07

Go 語言漢諾塔游戲

2023-03-27 00:20:48

2015-12-21 14:56:12

Go語言Http網絡協(xié)議

2012-11-08 09:36:10

Google Go

2021-03-01 18:35:18

Go語言虛擬機

2021-03-01 21:59:25

編程語言GoCX

2014-12-26 09:52:08

Go
點贊
收藏

51CTO技術棧公眾號