This is a creation in Article, where the information may have evolved or changed.
Follow up with the study, stay for reference
1, the core is the need to get the absolute path of the file, OS. Open () required;
2, Windows uses "\" to indicate the path, and go needs "/" to represent the path, so you need to replace "\" with "/";
3, use the built-in HTTP. The Serverfile () function provides subsequent file transfer services;
Package Mainimport ("FMT" "Log" "Net/http" "OS" "Path/filepath" "strings") var Staticdir = "/static" func Getcurdir () string {dir, err: = FilePath. Abs (filepath. Dir (OS. Args[0])) if err! = Nil {log. Fatal (ERR)}return strings. Replace (dir, "\ \", "/",-1)}func Dealstaticfiles (w http). Responsewriter, R *http. Request) {stdir: = Getcurdir () if strings. Hasprefix (R.url. Path, Staticdir) {file: = Stdir + R.url. Pathfmt.println (file) F, err: = OS. Open (file) defer f.close () if err! = Nil && os. Isnotexist (Err) {FMT. Fprintln (W, "File not Exist") return}http. Servefile (W, R, file) return} else {fmt. Fprintln (W, "Hello World")}}func Main () {http. Handlefunc ("/", Dealstaticfiles) //set access to the route err: = http. Listenandserve (": 8080", nil)//Set the listening port if err! = Nil {log. Fatal ("Listenandserve:", Err)}}