Government Security
Network Security Resources

Jump to content

Photo

Make A Basic Go Http Server


  • Please log in to reply
No replies to this topic

#1 Stephen

Stephen

    Commander In Chief

  • GSO Management
  • 1,787 posts

Posted 01 August 2010 - 11:37 PM

If you’ve read any of the Go Documentation there are a couple of examples of howto use “http” package. In particular if you’ve read the Effective Go Doc there’s an example that’s very similar to this implementation.

Starting with the Boilerplate example we need to add an import for “http” and add a couple of line to func main. to end up with the following :

package main

import (
	"fmt"
	"http"
)

func main(){
	fmt.Printf("Starting http Server ... ")
	http.Handle("/", http.HandlerFunc(sayHello))
	err := http.ListenAndServe("0.0.0.0:8080", nil)
	if err != nil {
		fmt.Printf("ListenAndServe Error :" + err.String())
	}

func sayHello(c *http.Conn, req *http.Request) {
	fmt.Printf("New Request\n")
	c.Write([]byte("<h1>Go Say's Hello</h1><h2>(Via http)</h2>"))
	c.Flush()
}

Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users