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
+9.5k Golang : Surveillance with web camera and OpenCV
+23.4k Golang : Record voice(audio) from microphone to .WAV file
+26.8k Golang : Login(Authenticate) with Facebook example
+6.6k Golang : Gonum standard normal random numbers example
+10.1k Android Studio : Password input and reveal password example
+3.6k Golang : Pad file extension automagically
+7.5k Golang : How to get ECDSA curve and parameters data?
+16.1k Golang : Logging with logrus
+6.5k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+4.7k Golang : Create new color from command line parameters
+40.9k Golang : Marshal and unmarshal json.RawMessage struct example
+12.2k Golang : concatenate(combine) strings