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
+14k Golang : syscall.Socket example
+21.5k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+14.3k Golang : Rename directory
+15.3k Golang : How to convert(cast) IP address to string?
+20.2k Android Studio : AlertDialog and EditText to get user string input example
+6.7k Golang : Calculate BMI and risk category
+5.5k Unix/Linux/MacOSx : Get local IP address
+8.8k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+6.1k Apt-get to install and uninstall Golang
+19.6k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+21.9k Golang : Securing password with salt
+12.3k Golang : Forwarding a local port to a remote server example