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
+11k Golang : Find location by IP address and display with Google Map
+9.1k Golang : Fix image: unknown format error
+6.2k Golang : Load ASN1 encoded DSA public key PEM file example
+5.2k Golang : Set or add headers for many or different handlers
+16.2k Golang : Match strings by wildcard patterns with filepath.Match() function
+3k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+10.9k Swift : Convert (cast) Int to String ?
+2.6k Javascript : How to show different content with noscript?
+32.3k Golang : Remove dashes(or any character) from string
+3.7k Golang : Convert Chinese UTF8 characters to Pin Yin
+13.2k Golang : Get file permission
+20.1k Golang : Record voice(audio) from microphone to .WAV file