Created
April 3, 2016 19:49
-
-
Save jbenner-radham/611a766bcbb2903d6e67d82e56ba91dc to your computer and use it in GitHub Desktop.
A simple "Hello World" web server written in Go.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"net" | |
"time" | |
) | |
func main() { | |
listener, err := net.Listen("tcp", ":8080") | |
if err != nil { | |
log.Fatal(err) | |
} | |
for { | |
conn, err := listener.Accept() | |
if err != nil { | |
log.Fatal(err) | |
} | |
conn.Write([]byte("HTTP/1.1 200 OK\r\n")) | |
conn.Write([]byte(fmt.Sprintf("Date: %s\r\n", time.Now().Format(time.RFC1123Z)))) | |
conn.Write([]byte("Content-Type: text/html\r\n")) | |
conn.Write([]byte(fmt.Sprintf("Content-Length: %d\r\n", len("Hello, world!")))) | |
conn.Write([]byte("\r\n")) | |
conn.Write([]byte("Hello, world!")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment