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
+5.5k Golang : Humanize and Titleize functions
+28k Golang : Get and Set User-Agent examples
+10.4k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+22.2k Golang : Use regular expression to validate domain name
+4.9k Golang : Function as an argument type example
+5.1k Golang : Selection sort example
+6.4k Golang : How to convert strange string to JSON with json.MarshalIndent
+10.9k Golang : Encrypt and decrypt data with x509 crypto
+24.8k Golang : Convert file content into array of bytes
+4.1k Unix/Linux : secure copying between servers with SCP command examples
+8k Golang : Temperatures conversion example