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
+16.8k Golang : Covert map/slice/array to JSON or XML format
+6.7k Golang : Find the shortest line of text example
+36.1k Golang : Convert date or time stamp from string to time.Time type
+5.3k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+8.4k Golang : On lambda, anonymous, inline functions and function literals
+14.1k Golang : How to check if your program is running in a terminal
+5.8k Golang : Compound interest over time example
+8.5k Golang : Get final balance from bit coin address example
+28k Golang : Change a file last modified date and time
+10k Golang : Convert file content to Hex
+21.9k Golang : Repeat a character by multiple of x factor