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.6k Golang : How to protect your source code from client, hosting company or hacker?
+5.5k Golang : Pad file extension automagically
+5.4k Javascript : Change page title to get viewer attention
+13.6k Golang : Get constant name from value
+7.6k Golang : Convert source code to assembly language
+43.9k Golang : Get hardware information such as disk, memory and CPU usage
+8.5k Golang : Number guessing game with user input verification example
+14.6k Golang : Parsing or breaking down URL
+5.5k Golang : Return multiple values from function
+24.7k Golang : Time slice or date sort and reverse sort example
+18.6k Golang : How to get hour, minute, second from time?