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
+3.9k Python : Convert(cast) bytes to string example
+11k Elastic Search : Return all records (higher than default 10)
+20.2k Golang : How to reverse slice or array elements order
+13.3k nginx: [emerg] unknown directive "ssl"
+5k Apt-get to install and uninstall Golang
+5.7k Golang : Null and nil value
+23.8k Golang : Get executable name behind process ID example
+10.8k Golang : How to display image file or expose CSS, JS files from localhost?
+25.5k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+7k Useful methods to access blocked websites
+27.3k Golang : How to create new XML file ?
+4.8k AWS S3 : Prevent Hotlinking policy