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
+11.6k Get form post value in Go
+6.2k Golang : Scan forex opportunities by Bollinger bands
+7.5k Android Studio : How to detect camera, activate and capture example
+40.2k Golang : UDP client server read write example
+7.9k Golang : Gomobile init produce "iphoneos" cannot be located error
+7.9k Swift : Convert (cast) String to Double
+17k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+17.7k Golang : [json: cannot unmarshal object into Go value of type]
+13.9k Golang : unknown escape sequence error
+5.4k Golang : Reclaim memory occupied by make() example
+15.3k Golang : Get query string value on a POST request
+15.6k Golang : rune literal not terminated error