用Google Go语言实现http共享

开发 开发工具
我们今天要介绍一下用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语言发布两周年 不断改进中
  2. Google Go:新兴语言的代表
  3. 1月编程榜发布:Google Go意外夺得年度编程语言
  4. Google Go有啥用?以及何谓好的系统编程语言
  5. Google Go语言的快乐编程因素
责任编辑:彭凡 来源: 博客园
相关推荐

2012-08-06 08:50:05

Go语言

2020-08-12 08:56:30

代码凯撒密码函数

2022-11-01 18:29:25

Go语言排序算法

2023-05-08 07:55:05

快速排序Go 语言

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命令

2013-12-12 10:55:21

2023-03-27 00:20:48

2022-04-18 10:01:07

Go 语言汉诺塔游戏

2023-07-31 08:01:13

二叉搜索测试

2021-07-26 09:47:38

Go语言C++

2015-12-21 14:56:12

Go语言Http网络协议

2021-03-01 18:35:18

Go语言虚拟机

2021-03-01 21:59:25

编程语言GoCX

2012-11-08 09:36:10

Google Go

2014-12-26 09:52:08

Go

2022-07-20 09:52:44

Go语言短信验证码

2023-12-07 12:59:46

C语言循环队列代码
点赞
收藏

51CTO技术栈公众号