[golang]文件服务器

    也来凑个热闹,学习Golang,摆脱苦逼的C++,上代码一个文件服务器。

package main

import (
    "os"
    "os/exec"
    "strings"
    "net/http"
)

func main(){
    h := http.FileServer(http.Dir(getCurrentPath()))
    http.ListenAndServe(":8080", h)
}

func getCurrentPath() string {
    s, err := exec.LookPath(os.Args[0])
    checkErr(err)
    i := strings.LastIndex(s, "\\")
    path := string(s[0 : i+1])
    return path
}

func checkErr(err error) {
    if err != nil {
        panic(err)
    }
}
在浏览器,输入:localhost:8080 就能看效果了,确实简单

你可能感兴趣的:(Go,golang)