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
+6.9k Golang : Load DSA public key from file example
+29.5k Golang : bufio.NewReader.ReadLine to read file line by line
+13.5k Golang : Execute function at intervals or after some delay
+12k Golang : Count number of runes in string
+6.8k Golang : Scan files for certain pattern and rename part of the files
+17k Golang : Iterating Elements Over A List
+8.4k Golang : ffmpeg with os/exec.Command() returns non-zero status
+16.5k Golang : Simple client server example
+16.8k Golang : Check if a directory exist or not
+61.4k Golang : Convert HTTP Response body to string
+14.3k Golang : How to convert(cast) IP address to string?
+9.7k Golang : Create Temporary File