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
+10.1k Golang : Convert octal value to string to deal with leading zero problem
+9.2k Golang : Capture text return from exec function example
+5.4k PHP : See installed compiled-in-modules
+7.6k Golang : Rename part of filename
+7.6k Golang : Detect sample rate, channels or latency with PortAudio
+17.8k Golang : [json: cannot unmarshal object into Go value of type]
+7.1k Golang : Takes a plural word and makes it singular
+5.3k Golang : Experimental Jawi programming language
+19.5k Golang : Get RGBA values of each image pixel
+18.1k Golang : Defer function inside init()
+18.2k Golang : Check if a directory exist or not
+6.2k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)