您好,欢迎访问沃航(武汉)科技有限公司官方网站
使用go语言写的不到30行的静态web服务器源码
2024-10-07 23:11:44

话不多说,代码见下面

package main

import (
    "flag"
    "fmt"
    "net/http"
    "strconv"
)

func main() {
    var httpport int
    var webpath string
    flag.IntVar(&httpport, "httpport", 80, "http服务器监听的端口号")
    flag.StringVar(&webpath, "webpath", ".", "http服务器www目录")
    flag.Parse()
    if httpport <= 0 || httpport > 65535 {
        fmt.Println("httpport参数错误")
        flag.Usage()
        return
    }
    fmt.Println("httpport:", httpport)
    fmt.Println("webpath:", webpath)
    http.Handle("/", http.FileServer(http.Dir(webpath)))
    http.ListenAndServe(":"+strconv.Itoa(httpport), nil)
}


文章作者:沃航科技

联系我们
地址:
武汉市洪山区蓝晶国际7栋903
QQ:
932773931
电话:
027-59761089-806
手机:
13397158231
邮箱:
jevian_ma@worldflying.cn
×
物联网组态平台
试用账号:123456
试用密码:123456
如需测试更多功能或者有疑问可发送邮件至:jevian_ma@worldflying.cn
×
积木编程平台
试用方式:试用手机号码注册即可使用
如需测试更多功能或者有疑问可发送邮件至:jevian_ma@worldflying.cn