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.7k Golang : How to tell if a file is compressed either gzip or zip ?
+19.2k Golang : Saving private and public key to files
+14.5k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+9.1k Golang : How to tokenize source code with text/scanner package?
+33.8k Golang : Get file last modified date and time
+21.1k Golang : Set and Get HTTP request headers example
+5k Golang : Calculate US Dollar Index (DXY)
+5.1k Golang : Missing Subversion command
+14.2k Golang : Get current time from the Internet time server(ntp) example
+20.7k Golang : How to run Golang application such as web server in the background or as daemon?
+26.6k Golang : Change a file last modified date and time
+13.1k Golang : Recombine chunked files example