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
+15.1k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+7.8k Golang : Configure Apache and NGINX to access your Go service example
+24k Golang : Time slice or date sort and reverse sort example
+6.8k Golang : Check if one string(rune) is permutation of another string(rune)
+6k Golang : Break string into a slice of characters example
+76.9k Golang : How to return HTTP status code?
+14.1k Golang : Find network of an IP address
+18.1k Golang : Implement getters and setters
+8.2k Golang : Progress bar with ∎ character
+10.2k Golang : Bubble sort example
+17.1k Golang : Parse date string and convert to dd-mm-yyyy format