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
+17.8k Golang : How to make function callback or pass value from function as parameter?
+12k Golang : List objects in AWS S3 bucket
+10.4k Golang : Generate DSA private, public key and PEM files example
+4.3k Golang *File points to a file or directory ?
+50.1k Golang : Check if item is in slice/array
+18.5k Golang : Append content to a file
+26.8k Golang : Connect to database (MySQL/MariaDB) server
+13.5k Golang : Find commonalities in two slices or arrays example
+6.7k Golang : Ways to recover memory during run time.
+9.2k Golang : cannot assign type int to value (type uint8) in range error
+11k Android Studio : Highlight ImageButton when pressed on example