Golang : Simple File Server
Got a request from a developer friend yesterday on how to open up part of his machine and turn into a file server. The code below is the easiest solution that I can come up with for his question.
package main
import (
"net/http"
)
func main() {
http.Handle("/", http.FileServer(http.Dir("./<whatever directory you want to expose>")))
http.ListenAndServe(":8108", nil)
}
What the code above did basically is to scan the target directory content with http.Dir()
function and send out the content to the web portion via http.
Hope this tutorial can be help to others.
See also : Golang : Read directory content with os.Open
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+14.6k Golang : Get URI segments by number and assign as variable example
+13.5k Golang : Image to ASCII art example
+7.1k Golang : Dealing with postal or zip code example
+12.3k Golang : "https://" not allowed in import path
+51.7k Golang : How to get time in milliseconds?
+6.3k PHP : Proper way to get UTF-8 character or string length
+10.2k Golang : cannot assign type int to value (type uint8) in range error
+9.1k Golang : Generate Codabar
+26.2k Golang : Calculate future date with time.Add() function
+8.5k Golang : Convert(cast) []byte to io.Reader type
+12.1k Golang : List running EC2 instances and descriptions
+9.6k Golang : Find correlation coefficient example