Skip to content

Instantly share code, notes, and snippets.

@gouthamve
Last active February 22, 2016 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gouthamve/7c507e49c083d3438ed1 to your computer and use it in GitHub Desktop.
Save gouthamve/7c507e49c083d3438ed1 to your computer and use it in GitHub Desktop.
A simple GET request in GoLang
package main
import (
"bufio"
"fmt"
"log"
"net"
)
func main() {
ExampleGETReq()
AlternateExampleGETReq()
}
func ExampleGETReq() {
addr, err := net.ResolveTCPAddr("tcp", "google.com:80")
if err != nil {
log.Fatal(err)
}
// Open a connection to google.com
conn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
log.Fatal(err)
}
// Write to the connection
fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
// Listen for reply and process it
status, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
log.Fatal(err)
}
fmt.Println("Status: ", status)
// Finally close the connection
conn.Close()
}
func AlternateExampleGETReq() {
// Open a connection to google.com
conn, err := net.Dial("tcp", "google.com:80")
if err != nil {
log.Fatal(err)
}
// Write to the connection
fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
// Listen for reply and process it
status, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
log.Fatal(err)
}
fmt.Println("Status: ", status)
// Finally close the connection
conn.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment