Golang : Muxing with Martini example
In this quick tutorial, we will learn how to do basic multiplexing with Martini package. One thing I really like about Martini package is that you can achieve many common tasks with just one or two lines. Learn more about this package at http://martini.codegangsta.io/. Meanwhile, this is the code example on how to do multiplexing with Martini.
package main
import (
"fmt"
"github.com/go-martini/martini"
"net/http"
)
func Home(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
}
func SayHello(w http.ResponseWriter, r *http.Request, params martini.Params) {
w.Write([]byte(fmt.Sprintf("Hello, %s!", params["name"])))
}
func main() {
mx := martini.Classic()
mx.Get("/", Home)
mx.Get("/:name", SayHello)
// martini default port number is 3000
// example on how to change to 8080
mx.RunOnAddr(":8080")
http.Handle("/", mx)
}
Run the code and point your browser to http://localhost:8080/Jeff
Sample output :
http://localhost:8080
Hello, World!
http://localhost:8080/Jeff
Hello, Jeff!
go run martinimux.go
[martini] listening on :8080 (development)
[martini] Started GET / for [::1]:65494
[martini] Completed 200 OK in 234.235µs
[martini] Started GET /Jeff for [::1]:65494
[martini] Completed 200 OK in 95.226µs
Reference :
See also : Golang : Routes multiplexer routing example with regular expression control
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.6k Golang : Aligning strings to right, left and center with fill example
+7.8k Golang : Accept any number of function arguments with three dots(...)
+35.7k Upload multiple files with Go
+4.7k Golang : Get S3 or CloudFront object or file information
+8.5k Facebook : Getting the friends list with PHP return JSON format
+8.7k Golang : Turn string or text file into slice example
+20.7k Golang : How to reverse slice or array elements order
+12.8k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+12.6k Golang : Reverse IP address for reverse DNS lookup example
+6.1k Golang : Accessing dataframe-go element by row, column and name example
+13k Golang : Chunk split or divide a string into smaller chunk example
+4.5k Javascript : How to loop over and parse JSON data?