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
+8.1k Golang : Generate Codabar
+12.1k Golang : Image to ASCII art example
+12.9k Golang : Check if a file exist or not
+18.5k Golang : Append content to a file
+3.8k Golang : Experimental Jawi programming language
+4.5k Clean up Visual Studio For Mac installation failed disk full problem
+10.8k Golang : Print UTF-8 fonts on image example
+16.7k Golang : Get command line arguments
+14.9k Golang : Read integer from file into array
+8.8k PHP : Get coordinates latitude/longitude from string
+3.8k JavaScript : Rounding number to decimal formats to display currency
+24.8k Golang : Convert(cast) string to uint8 type and back to string