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
+13k Golang : Date and Time formatting
+7.3k Golang : Rename part of filename
+11.4k Golang : Calculations using complex numbers example
+12.1k Golang : Search and extract certain XML data example
+20.1k Android Studio : AlertDialog and EditText to get user string input example
+17.7k Golang : Put UTF8 text on OpenCV video capture image frame
+22.7k Golang : Read a file into an array or slice example
+30.2k Golang : Remove characters from string example
+6.1k CodeIgniter : form input set_value cause " to become & quot
+10.6k Golang : Natural string sorting example
+9.4k Golang : Format strings to SEO friendly URL example