Motyar

@motyar

Freelance Web Developer

Static Web Hosting, made easy

Sep 8, 2014

Creating Static WebServer in Go ( golang )

I am learning golang, I have go through the golang tour

I found it a very easy and fast language for web backend.

In this post I want to share a very simple static file server we can create in few line of Go (golang)

Here is the code you have to put in server.go ( or any name dot go)


package main                                                          
                                                                      
import (                                                              
    "log"                                                             
    "net/http"                                                        
)                                                                     
                                                                      
func main() {                                                         
    // Simple static webserver:                                       
    log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("."))))                                                                           
} 

You can change the dot with any path or dir that you want to server on port 8080.
Port can be anything too. Run this code like this

go run server.go

Now open http://localhost:8080/ or http://IPofYourVPN:8080

Labels: ,




By :